简体   繁体   中英

Pass all parameters of one shell script to another

I have a shell script to which i am passing few parameters. Test1.sh -a 1 -b 2 -c "One Two Three"

Inside Test1.sh i am calling another shell script in the below fashion. Test2.sh $*

I want to pass all the parameters to Test2, that were passed to Test1 and also in the same format (with double quotes etc). However the parameters that get passed to Test2 are Test2.sh -a 1 -b 2 -c One Two Three which doesnt work for me. Is there a way around it so that i can pass the parameters the same way as I am passing to Test1.

Thanks Zabi

You need to say:

Test2.sh "$@"

Refer to Special Parameters :

@

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... . If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (ie, they are removed).

The manual says:

"$*" is equivalent to "$1c$2c..." , where c is the first character of the value of the IFS variable.

which explains the result you're observing.

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