Why use echo before installing new software?

5,203

Solution 1

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.)

Solution 2

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.

Share:
5,203

Related videos on Youtube

Pallav Bakshi
Author by

Pallav Bakshi

Hi, I'm a Data Science expert with hands-on Machine Learning and Data Engineering skills; along with strong experience and interest in business strategy. I've worked with cross-functional teams, building strategies, KPIs and dashboards for marketing, product and senior leadership. I have worked with all the aspects of data: Data Analysis Created reports for CEO to present to his investors. Analyzed future business opportunities for our company. Performed ad-hoc analysis. Data Engineering Build distributed pipelines to collect data from APIs and store them in data lake. Created data cleaning and transformation pipeline to clean data from data lake. Create data warehousing solution using BigQuery to present strucutred data to business. Data Science Build dashboard using Google Data Studio to help democratize data across the company. Helped marketing team and product team come up with KPIs. Presented upcoming data opportunities to CEO, VP of Engineering, etc. Machine Learning Engineering [CORE SKILL] Build machine learning pipeline using sklearn and pandas Build deep learning models using Tensorflow and MxNet Research and code custom loss functions leading to 7% improvement in model accuracy Hackathon Won TWO hackathons; conceptualizing, building and presenting

Updated on September 18, 2022

Comments

  • Pallav Bakshi
    Pallav Bakshi over 1 year

    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

  • dirkt
    dirkt over 7 years
    Actually, > overwrites the text file, >> appends to the end.
  • Admin
    Admin over 7 years
    @dirkt Thanks. My bad! ;) That's the consequence of learning several scripting languages. The conflicting behavior eventually come to haunt you. Even in DOS, > overwrites.
  • Michael Hampton
    Michael Hampton over 7 years
    echo writes to standard output, not the console.
  • Admin
    Admin over 7 years
    "Unless redirected, standard output is the text terminal which initiated the program." So, yes, echo writes to the console.