简体   繁体   中英

Pass all args to a command called in a new shell using bash -c

I've simplified my example to the following:

file1.sh:

#!/bin/bash
bash -c "./file2.sh $@"

file2.sh:

#!/bin/bash
echo "first $1"
echo "second $2"

I expect that if I call ./file1.sh ab to get:

first a
second b

but instead I get:

first a
second

In other words, my later arguments after the first one are not getting passed through to the command that I'm executing inside a new bash shell. I've tried many variations of removing and moving around the quotation marks in the file1.sh file, but haven't got this to work.

Why is this happening, and how do I get the behavior I want?

(UPDATE - I realize it seems pointless that I'm calling bash -c in this example, my actual file1.sh is a proxy script for a command that gets called locally to run in a docker container so it's actually docker exec -i mycontainer bash -c '' )

Change file1.sh to this with different quoting:

#!/bin/bash
bash -c './file2.sh "$@"' - "$@"

- "$@" is passing hyphen to populate $0 and $@ is being passed in to populate all other positional parameters in bash -c command line.

You can also make it:

bash -c './file2.sh "$@"' "$0" "$@"

However there is no real need to use bash -c here and you can just use:

./file2.sh "$@"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM