Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagate all arguments in a bash shell script

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?

like image 230
Fragsworth Avatar asked Jan 28 '11 03:01

Fragsworth


People also ask

How do I pass multiple arguments to a shell script?

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 …

How do you assign a all the arguments to a single variable?

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.

What does [- Z $1 mean in bash?

$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.


2 Answers

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: 
like image 52
Sdaz MacSkibbons Avatar answered Oct 31 '22 02:10

Sdaz MacSkibbons


For bash and other Bourne-like shells:

java com.myserver.Program "$@" 
like image 26
Chris Johnsen Avatar answered Oct 31 '22 01:10

Chris Johnsen