Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

shell

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?

like image 747
Yuvaraj V Avatar asked Dec 06 '22 16:12

Yuvaraj V


2 Answers

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.
like image 107
anubhava Avatar answered Jan 13 '23 12:01

anubhava


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
like image 29
uupascal Avatar answered Jan 13 '23 13:01

uupascal