1053

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?

1
  • 3
    Possible duplicate of Propagate all arguments in a bash shell script. (This question was actually posted before the one linked here. But the one in the link has more detailed answers and a more informative title and may be best as the reference question) Commented Oct 25, 2019 at 21:46

7 Answers 7

1757

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

Passing some arguments:

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.

Things you probably don't want to do:

"$*" 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.)

Sign up to request clarification or add additional context in comments.

10 Comments

Read more on why it is important to have the double " around here: stackoverflow.com/a/4824637/4575793
"${@:3}" doesn't seem to work with zsh.
Extra comment: $* will be a single string with the first character of IFS being the concatenating element.
@kvantour That's what happens if $* is in double-quotes. If it's unquoted, it's subject to word splitting and wildcard expansion, so it immediately gets re-split back to the elements and any elements that contain $IFS characters will also get split (and then any wildcards get expanded). Net result: without double-quotes, $* and $@ wind up giving the same result.
@GordonDavisson exactly. I found this information missing, but it does not belong in your great answer. A comment like it is now is better.
|
174

I needed a variation on this, which I expect will be useful to others:

function diffs() {
        diff "${@:3}" <(sort "$1") <(sort "$2")
}

The "${@:3}" part means all the members of the array starting at 3. So this function implements a sorted diff by passing the first two arguments to diff through sort and then passing all other arguments to diff, so you can call it similarly to diff:

diffs file1 file2 [other diff args, e.g. -y]

3 Comments

The "${@:3}" is also great when you have scripts that have arguments, but can also pass arguments to other scripts that they call. For example, a project of mine has a script for easily running the program, with an argument for the main class to use. Then "${@:2}" can be used to pass the remaining arguments to that entry point.
@Kat already mentioned this, but to clarify (in case you still have doubts): command "$@" is equivalent to command $1 "${@:2}".
OMG! I kiss you!
74

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

abc "$@"

2 Comments

This will break up arguments in quotes into separate arguments though, which is very rarely what one expects when running a script.
No, my mistake. It works when you quote the $@ as in your example, but not when using it without quotes as in the descriptive text.
68

Here's a simple script:

#!/bin/bash

args=("$@")

echo Number of arguments: $#
echo 1st argument: ${args[0]}
echo 2nd 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]}.

5 Comments

What benefit does passing $@ into an array provide over just calling the arguments by index (for example, $1)?
@Kingand: it allows to easily iterate over the arguments.
Remember: ${args[0]} is equivalent to $1 and not $0. So, if you want a script to always be run as root, you would put if [[ $(id -u) -ne 0 ]]; then sudo "$0" "$@"; exit 0; fi at the beginning.
Any future readers should note that the shebang in this example is incorrect. sh does not support arrays, that is a bash feature. The only reason this could work is if your OS has symlinked /bin/sh to bash or you call the script with bash script.sh.
@WhiteAbeLincoln: good point, I edited my answer.
50

It's worth mentioning that you can specify argument ranges with this syntax.

function example() {
    echo "line1 ${@:1:1}"; #First argument
    echo "line2 ${@:2:1}"; #Second argument
    echo "line3 ${@:3}"; #Third argument onwards
}

I hadn't seen it mentioned.

4 Comments

You can just use $1, $2,... for the first two
@rubo77 I've corrected the wording of my answer to include "range" thanks.
thanks, this is exactly what I needed for indexing $@ with a variable
how would this behave when order matters. Upvoted this...
35
abc "$@"

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

2 Comments

If you don't quote $@ you will lose the correct word splitting
@Daenyth is right, read more about this here: stackoverflow.com/a/4824637/4575793
35

abc "$@" is generally the correct answer. But I was trying to pass a parameter through to an su command, and no amount of quoting could stop the error su: unrecognized option '--myoption'. What actually worked for me was passing all the arguments as a single string :

abc "$*"

My exact case (I'm sure someone else needs this) was in my .bashrc

# run all aws commands as Jenkins user
aws ()
{
    sudo su jenkins -c "aws $*"
}

4 Comments

Hey OP, looks like you may have a typo on the abc example
Exactly what I needed. My command was wrapped in quotes and this was the only thing that worked for me.
Probably the way to use, but be aware of the risk when you use string arguments. Please look at the "$*" example here: stackoverflow.com/a/46205560/4575793 (This is more on '$@' vs '$*' plus there quoted variants)
I’m trying to understand the difference: in both cases bash expands the variable (either $@ or $*) before passing the resulting, expanded string to su. So why does one string contain all arguments (expanded from $*) and the other does not (expanded from $@)? 🤔

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.