1

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?

terdon
  • 98,183
  • 15
  • 197
  • 293
UgaUga
  • 29
  • 1
  • 3
  • 2
    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. – pLumo Jan 09 '19 at 17:10
  • 2
    @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. – wjandrea Jan 09 '19 at 18:47
  • @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. – UgaUga Jan 09 '19 at 18:56
  • 1
    [How can I get help on terminal commands?](https://askubuntu.com/q/991946/507051) – dessert Jan 09 '19 at 19:00
  • sh man were pretty long, so am I a gambler – UgaUga Jan 09 '19 at 19:01
  • @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?](https://askubuntu.com/q/831847/301745), [How do I add a line to my /etc/apt/sources.list?](https://askubuntu.com/q/197564/301745)), but they're hard to find unless you know what you're looking for. – wjandrea Jan 09 '19 at 19:08

3 Answers3

4
$ 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.

Thomas Ward
  • 72,494
  • 30
  • 173
  • 237
0

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.
wjandrea
  • 14,109
  • 4
  • 48
  • 98
Daniele Santi
  • 3,084
  • 4
  • 30
  • 30
  • 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. =) – UgaUga Jan 09 '19 at 17:41
0

For the beginers:

  1. sh (shell) is a command interpreter program. Like Bash "Bourne Again SHell" is the GNU Project's shell.

  2. curl or cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name stands for "Client URL".

Vijay
  • 7,606
  • 3
  • 17
  • 34