Why Do People Use the “echo” Command When Installing Software in Linux?

Why people use the echo command when installing sofeware in linux

There are many of the commands and variations thereof may seem a bit confusing if you are new in using Linux. Take the “echo” command, for example. Why do people use it when installing software? Today’s SuperUser Q&A post has the answer to a new Linux user’s question.

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

The Question

SuperUser reader PallavBakshi wants to know why people use the “echo” command when installing software in Linux:

I am new to the computing world. While installing ROS Indigo, the first step said that I should use the following code:

sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main” > /etc/apt/sources.list.d/ros-latest.list’

Why do people use the “echo” command along with “sh -c” in this context? I have seen the “echo” command used in other installation processes as well.

 

The Answer

SuperUser contributor Fleet Command has the answer for us:

Ordinarily, the function of the “echo” command is to display a string (piece of text) on the console. But this time, a (greater than) > character is added after the echo command, redirecting its output to a text file located in /etc/apt/sources.list.d/ros-latest.list.

Basically, this whole command writes a piece of text to a text file (overwrites the text file and appends information to the end). Now, here comes the tricky part:

The string written to the file may be different for each computer. The part, $(lsb_release -sc), is resolved (changed into something else) when the “echo” command runs.

You can open /etc/apt/sources.list.d/ros-latest.list in a text editor before and after the command to see the changes for yourself. Keep in mind that the file might not exist before using this command.

Leave a Reply

Your email address will not be published. Required fields are marked *