Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass all parameters of one shell script to another

Tags:

shell

unix

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

like image 694
user2726057 Avatar asked Aug 28 '13 15:08

user2726057


1 Answers

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 (i.e., 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.

like image 110
devnull Avatar answered Nov 30 '22 05:11

devnull