I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.
For instance, my script name is foo.sh
and calls bar.sh
foo.sh:
bar $1 $2 $3 $4
How can I do this without explicitly specifying each parameter?
To pass multiple arguments to a shell script you simply add them to the command line: # somescript arg1 arg2 arg3 arg4 arg5 … To pass multiple arguments to a shell script you simply add them to the command line: # somescript arg1 arg2 arg3 arg4 arg5 …
Assigning the arguments to a regular variable (as in args="$@" ) mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.
Use "$@"
instead of plain $@
if you actually wish your parameters to be passed the same.
Observe:
$ cat no_quotes.sh #!/bin/bash echo_args.sh $@ $ cat quotes.sh #!/bin/bash echo_args.sh "$@" $ cat echo_args.sh #!/bin/bash echo Received: $1 echo Received: $2 echo Received: $3 echo Received: $4 $ ./no_quotes.sh first second Received: first Received: second Received: Received: $ ./no_quotes.sh "one quoted arg" Received: one Received: quoted Received: arg Received: $ ./quotes.sh first second Received: first Received: second Received: Received: $ ./quotes.sh "one quoted arg" Received: one quoted arg Received: Received: Received:
For bash and other Bourne-like shells:
java com.myserver.Program "$@"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With