Why use echo before installing new software?
I am new to the computing world. While installing ROS Indigo, the first step directs me to 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 we use the echo
command along with sh -c
in this context? I have seen the use of echo command in other installation processes as well.
Note - Use of sh command, ROS Indigo Installation Wiki
Ordinarily the function of echo
command is to display a string (piece of text) on the console. But this time, a >
character is added after the echo
command, redirecting its output to a text file, /etc/apt/sources.list.d/ros-latest.list
.
So basically, this whole command writes a piece of text to a text file. 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 change for yourself. (The file might not exist before this command.)
Package Management and Software Repositories
Debian based Linux distributions rely on repositories (databases of application installation packages and upgrade packages) to keep the operating systems updated and also to easily fetch and install new software packages. The location of these repositories are stored in /etc/apt/sources.list
, however additional sources, usually unique to specific applications can be stored in the /etc/apt/sources.list.d
directory.
When the package index update command apt-get update
is executed, your operating system checks with these package repositories for available packages and registers the available softwares as available to your operating system which you can go on to install using the traditional apt-get install <package>
command.
An example of one of these software sources is:
deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty main restricted
It is important for these sources to reference specific versions of linux distributions. An example is trusty which is the codename for Ubuntu 14.04. You can query your OS (debian based) for complete details with lsb_release -a
or lsb_release -sc
which means short and codename.
In your question, the part $(lsb_release -sc)
is interpreted and the result from your operating system is printed into the custom source file ros-latest.list which the command will create upon execution.
Command language interpreter
The sh
command is the bourne shell. This is one, among several shells but is considered the old standard and generally one you can be certain exist. It's also common to see bash
in many shell scripts. That declaration is specifying the shell to use as different shells use different syntax.
As regards the -c
flag, quoting man bash
:
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
Everything within the ''
is read as a string, you wouldn't need to figure out how to escape various quotation marks or worry about the shell interpreting something the wrong way.
tl;dr
The command prints deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main
to a custom source file, replacing $(lsb_release -sc)
with the interpreted value.