tags:

views:

65

answers:

4

Let's say I have defined a function abc() that will handle all the logic related to analising the arguments passed to my script.

How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode the arguments passed like this:

abc $1 $2 $3 $4

edit: Better yet, is there any way for my function to have access to the script arguments' variables?

Thanks

A: 
abc $@

$@ represents all the parameters given to your bash script.

Vivien Barousse
If you don't quote `$@` you will lose the correct word splitting
Daenyth
+1  A: 

Use the "$@" variable, which expands to all command-line parameters separated by spaces.

abc "$@"
Banang
A: 

Here's a simple script:

#/bin/sh

args=("$@")

echo Number of arguments: $#
echo First argument: ${args[1]}

$# is the number of arguments received by the script. I find easier to access them using an array: the args=("$@") line puts all the arguments in the args array. To access them use ${args[index]}.

Giuseppe Cardone
+4  A: 

Pet peeve: when using $@, you should (almost) always put it in double-quotes to avoid misparsing of argument with spaces in them:

abc "$@"
Gordon Davisson