105

I know how to execute remote Bash scripts like this:

curl http://example.com/script.sh | bash

or

bash < <( curl http://example.com/script.sh )

which give the same result.

But what if I need to pass arguments to the bash script? It's possible when the script is saved locally:

./script.sh argument1 argument2

I tried several possibilities like this one, without success:

bash < <( curl http://example.com/script.sh ) argument1 argument2

4 Answers 4

153

To improve on jinowolski's answer a bit, you should use:

curl http://example.com/script.sh | bash -s -- arg1 arg2

Notice the two dashes (--) which are telling bash to not process anything following it as arguments to bash.

This way it will work with any kind of arguments, e.g.:

curl -L http://bootstrap.saltstack.org | bash -s -- -M -N stable

This will of course work with any kind of input via stdin, not just curl, so you can confirm that it works with simple BASH script input via echo:

echo 'i=1; for a in $@; do echo "$i = $a"; i=$((i+1)); done' | \
bash -s -- -a1 -a2 -a3 --long some_text

Will give you the output

1 = -a1
2 = -a2
3 = -a3
4 = --long
5 = some_text
Sign up to request clarification or add additional context in comments.

5 Comments

An excellent suggestion, and very clear as to the reason why you should do this.
Thanks for the clear explanation. This trick is really useful!
for extra safety, it's advisable to use curl --fail .... It makes curl stop on a 404, for example. A common set of options is -fsSL (-f is the short version of --fail).
what if the command fail with because it asks a yes or no question?
Thanks! This is the only solution that worked for me, ie curl -fsSL https://starship.rs/install.sh | bash -s -- --yes.
104

try

curl http://foo.com/script.sh | bash -s arg1 arg2

bash manual says:

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.

5 Comments

Thanks ! Extremely useful with gists :)
What about parameters that use keys like -p blah -d blah?
The read in script is directly ignored by this way, how to fix this?
@hwding My answer below features that stackoverflow.com/a/72207423/4374258
does bash get confused if the argument is a "--parameter" to the script that is being curled? if so, how would we fix that?
21

Other alternatives:

curl http://foo.com/script.sh | bash /dev/stdin arguments
bash <( curl http://foo.com/script.sh ) arguments

3 Comments

All methods differ on $0 argument. For "-s" it's "bash", for "/dev/stdin" it's "/dev/stdin" and "<(...)" gives $0 argument like "/dev/fd/63".
First alternative works with script arguments beginning with a -, i.e. options
Please give an example for a argument to let me know the format
-1

Building on others' answers, if you want your bash script to use pipes, try:

cat myfile.txt | \
bash -c "$(curl http://example.com/script.sh )" -s arg1 arg2

Example usage:

#!/usr/bin/env bash

export MYURL="https://raw.githubusercontent.com/sohale/snippets/master/bash-magic/add-date.sh"
curl http://www.google.com | \
bash -c "$(curl -L $MYURL )" -s "       >>>>> next line 🕶👉"

If you use bit.ly to shorten the url, don't forget the -L.

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.