Let's say I have a function abc()
that will handle the logic related to analyzing the arguments passed to my script.
How can I pass all arguments my Bash script has received to abc()
? The number of arguments is variable, so I can't just hard-code the arguments passed like this:
abc $1 $2 $3 $4
Better yet, is there any way for my function to have access to the script arguments' variables?
The $@
variable expands to all command-line parameters separated by spaces. Here is an example.
abc "$@"
When using $@
, you should (almost) always put it in double-quotes to avoid misparsing of arguments containing spaces or wildcards (see below). This works for multiple arguments. It is also portable to all POSIX-compliant shells.
It is also worth noting that $0
(generally the script's name or path) is not in $@
.
The Bash Reference Manual Special Parameters Section says that $@
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" "$3"...
.
If you want to pass all but the first arguments, you can first use shift
to "consume" the first argument and then pass "$@"
to pass the remaining arguments to another command. In Bash (and zsh and ksh, but not in plain POSIX shells like dash), you can do this without messing with the argument list using a variant of array slicing: "${@:3}"
will get you the arguments starting with "$3"
. "${@:3:4}"
will get you up to four arguments starting at "$3"
(i.e. "$3" "$4" "$5" "$6"
), if that many arguments were passed.
"$*"
gives all of the arguments stuck together into a single string (separated by spaces, or whatever the first character of $IFS
is). This loses the distinction between spaces within arguments and the spaces between arguments, so is generally a bad idea. Although it might be ok for printing the arguments, e.g. echo "$*"
, provided you don't care about preserving the space within/between distinction.
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. Note that in Bash and ksh, array indexes start at 0, so $1
will be in args[0]
, etc. zsh, on the other hand, starts array indexes at 1, so $1
will be in args[1]
. And more basic shells like dash don't have arrays at all.
Leaving off the double-quotes, with either $@
or $*
, will try to split each argument up into separate words (based on whitespace or whatever's in $IFS
), and also try to expand anything that looks like a filename wildcard into a list of matching filenames. This can have really weird effects, and should almost always be avoided. (Except in zsh, where this expansion doesn't take place by default.)