curl, sh, what do those commands mean?

6,331

Solution 1

CURL

Like the homepage says, curl is a

command line tool and library for transferring data with URLs

Greatly simplyfing, it allows you to download a file from a (web)server.

You could get the same result by opening https://packagecloud.io/AtomEditor/atom/gpgkey with a browser and then saving the displayed file to disk.

SH

Running sh opens a new shell. The way it's used here is to execute a list of commands (via the -c flag) in a new shell with root privileges (the sudo part).

The sh -c part is needed because of the redirection (> /etc/apt/sources.list.d/atom.list). As the file /etc/apt/sources.list.d/atom.list needs root privileges to be written to, you cannot simply do sudo echo ... > file, as the redirection doesn't "inherit" privileges from the sudo part. You have to wrap the whole echo + > it in a new shell instance. It's somewhat equivalent to these separate steps:

  1. sudo sh to open a new shell with root privileges;
  2. echo "deb [arch=amd64] https://packagecloud.io/AtomEditor/atom/any/ any main" > /etc/apt/sources.list.d/atom.list to write the new line to the atom.list file;
  3. exit to go back to your normal user shell.

Solution 2

$ curl -sL https://packagecloud.io/AtomEditor/atom/gpgkey | sudo apt-key add -

This is actually two commands.

curl -sL https://packagecloud.io/AtomEditor/atom/gpgkey downloads the GPG key from PackageCLoud for the Atom Editor repository.

sudo apt-key add - adds it to apt so it can recognize and validate the repository's GPG signatures on packages.


$ sudo sh -c 'echo "deb [arch=amd64] https://packagecloud.io/AtomEditor/atom/any/ any main" > /etc/apt/sources.list.d/atom.list'

Easier if we split it into its three constituent parts.

sudo executes the sh command as superuser.

sh -c indicates to execute a specific command in the sh shell.

'echo "deb [arch=amd64] https://packagecloud.io/AtomEditor/atom/any/ any main" > /etc/apt/sources.list.d/atom.list' is the command being run by sh -c which creates the separate repository entry in /etc/apt/sources.list.d/atom.list so that when you do sudo apt update it'll check that repository for package data.

Share:
6,331

Related videos on Youtube

UgaUga
Author by

UgaUga

Updated on September 18, 2022

Comments

  • UgaUga
    UgaUga over 1 year

    I've been wandering in the web and saw this about how to install atom, the new text editor:

    $ curl -sL https://packagecloud.io/AtomEditor/atom/gpgkey | sudo apt-key add -
    $ sudo sh -c 'echo "deb [arch=amd64] https://packagecloud.io/AtomEditor/atom/any/ any main" > /etc/apt/sources.list.d/atom.list'
    

    I just wanted to know what these commands are actually doing. What does curl do ?

    I also read sh was about running some shell instance but for what, what does this command make possible for instance, and what does it do specifically here?

    • pLumo
      pLumo over 5 years
      Why the downvote? Appart from "what do curl do" (OP could easily figure out by himself), I think this is a valid question. I rather think it's very good if beginners want to know what they are copy-pasting from the internet, and we should not discourage that by downvoting.
    • wjandrea
      wjandrea over 5 years
      @RoVo I didn't downvote, but the tooltip for the downvote says "This question does not show any research effort; it is unclear or not useful", and this question doesn't show any research effort.
    • UgaUga
      UgaUga over 5 years
      @wjandrea lol that's partly true but the sh -c command still was quite tough to understand don't you find? When you understand nothing that's hard to understand any part you know.
    • dessert
      dessert over 5 years
    • UgaUga
      UgaUga over 5 years
      sh man were pretty long, so am I a gambler
    • wjandrea
      wjandrea over 5 years
      @UgaUga Yeah, it's three layers of commands deep, and it's got a lot going on apart from that too. There are some existing related questions (What is the sh -c command?, How do I add a line to my /etc/apt/sources.list?), but they're hard to find unless you know what you're looking for.
  • UgaUga
    UgaUga over 5 years
    ok! so it runs another shell in order to override the redirection restriction of 'echo', now I understand more this kind of 'wrapping' and the reason we do this, that was unusual for me! thank you. =)