0

What is the difference between running scripts like this?

curl -L https://url.com/install.sh/script | sh
curl -L https://url.com/install.sh/script | bash

And

curl https://url.com/install.sh/script > install.sh && sh ./install.sh
curl https://url.com/install.sh/script > install.sh && bash ./install.sh

EDIT: the reason I'm asking is that the first method gives me syntax errors, the second method works just fine.

NoobyAFK
  • 307
  • 4
  • 14
  • 1
    If you're getting errors, it would easier to get help if you tell us what the errors are. – root Jul 29 '19 at 00:08

2 Answers2

2

You're using two different mechanisms of Unix shells here:

  • | is a pipe, it passes stdout of one process on to stdin of another process; thus, when executing your first command, you're downloading content which gets printed to stdout with curl, then you execute sh/bash with that downloaded content on its stdin

  • > is a redirection, it places stdout of a process in the file following the operator (in your case install.sh); then, another command is called (they are combined with a logical &&, i.e. the first command must exit with a success status) which calls sh/bash, but this time with that exact file as their input

Usually, one would assume that both methods are achieving the same thing.

Daniel Schütte
  • 578
  • 1
  • 6
  • 20
  • thats what i was thinking too, but yet again i get syntax errors when using pipe, and no errors when using > – NoobyAFK Jul 28 '19 at 19:25
  • Can you provide the actual URL you're making the request to or is the install script confidental? I think without seeing that script (or at least the error messages you get), it's hard to find an answer to your question. – Daniel Schütte Jul 28 '19 at 20:58
1

In the first case, the script is passed directly to the shell (sh/bash) during the download and executed without being placed on a hard disk/SSD beforehand.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • the reason I'm asking is that the first method gives me syntax errors, the second method works just fine. – NoobyAFK Jul 28 '19 at 10:20
  • 1
    The second variant has the advantage that the script is only executed if curl was able to download the script completely. – Cyrus Jul 28 '19 at 10:30
  • Why are you/are you not providing the `-L` option to `curl`? Could that be causing your error? You should [check if you need that option or not](https://curl.haxx.se/docs/manpage.html#-L). – Daniel Schütte Jul 28 '19 at 10:33
  • 1
    same thing even without -L – NoobyAFK Jul 28 '19 at 19:25