Passing parameters to bash when executing a script fetched by curl

前端 未结 3 874
小鲜肉
小鲜肉 2020-12-07 09:18

I know how to execute remote Bash scripts like this:

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

or

bash < <( curl http:         


        
3条回答
  •  北海茫月
    2020-12-07 09:26

    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
    

提交回复
热议问题