Yuvaraj V
Yuvaraj V

Reputation: 1212

How can I pass arguments to a script executed by sh read from stdin?

I download some shell script from example.com with wget and execute it immediately by streaming stdout of wget via a pipe to stdin of the sh command.

wget -O - http://example.com/myscript.sh | sh -

How can I pass arguments to the script?

Upvotes: 6

Views: 4470

Answers (2)

phoehnel
phoehnel

Reputation: 839

While the accepted answer is correct, it does only work on bash and not on sh as the initial poster requested.

To do this in sh you'll have to add --:

curl https://example.com/script.sh | sh -s -- --my-arg

Upvotes: 13

anubhava
anubhava

Reputation: 785146

You need to use -s option while invoking bash for passing an argument to the shell script being downloaded:

wget -O - http://example.com/myscript.sh | bash -s 'arg1' 'arg2'

As per man bash:

-s   If the -s option is present, or if no arguments remain after option processing,
     then commands are  read  from the  standard  input. This option allows
     the positional parameters to be set when invoking an interactive shell.

Upvotes: 10

Related Questions