8

I'm running a bash script using

wget -O - https://myserver/install/Setup.sh | bash

How can I pass a parameter to the above script so it runs? something like

wget -O - https://myserver/install/Setup.sh parameter1 | bash
1
  • Like that, yes. Provided that which is fetched by wget is of course valid input for bash. Commented Jul 7, 2019 at 0:56

3 Answers 3

9

You can also run your script with:

wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1

See: man bash OPTIONS -s

   -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 or
             when reading input through a pipe.

or alternatively use the -c option.

bash -c "$(wget -qO - 'https://myserver/install/Setup.sh')" '' parameter1

the '' defines the parameter $0 to be empty string. In a normal file based script invocation, the parameter $0 contains the caller script name.

See: man bash OPTIONS -c

   -c        If the -c option is present, then commands are read from
             the first non-option argument command_string.  If there are
             arguments after the command_string, the first argument is
             assigned to $0 and any remaining arguments are assigned to
             the positional parameters.  The assignment to $0 sets the
             name of the shell, which is used in warning and error
             messages.
Sign up to request clarification or add additional context in comments.

1 Comment

Can use bash -- so that all parameters that follow are passed to the script and not interpreted by bash command e.g. for script parameter --param2 would be bash -s -- --param2
6

The standard format for the bash (or sh or similar) command is bash scriptfilename arg1 arg2 .... If you leave off all the first argument (the name or path of the script to run), it reads the script from stdin. Unfortunately, there's no way to leave off the firs argument but pass the others. Fortunately, you can pass /dev/stdin as the first argument and get the same effect (at least on most unix systems):

wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1

If you're on a system that doesn't have /dev/stdin, you might have to look around for an alternative way to specify stdin explicitly (/dev/fd/0 or something like that).

Edit: Léa Gris suggestion of bash -s arg1 arg2 ... is probably a better way to do this.

2 Comments

@GordonDavisson You missed about the bash -s parameter1 option and the bash -c command parameter0 parameter1 alternatives that may be more system agnostic. I documented these in my answer below
@LéaGris I did completely miss the -s option; nice!
0

I was not able to get either of the current 2 answers to work for me.

Thank you for the comment on the answer by @LéaGris from @Cas. That was the answer for me. Credit goes to @Cas for the parameter passing.

All of the other examples would quietly fail. Adding the -- solved it.

This question and its current answers are nearly 6 years old now in early 2025. Bash has seen some updates. I am currently using GNU bash, version 5.2.15(1) on Debian 12.9. I am also using a newer syntax for passing the script to bash.

bash < <(wget -qO - https://example.com/script.sh) -s -- <parameter>

...where <parameter> is any parameter or parameters you would normally pass to the script locally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.