Linux: Refer to all arguments of a script call

Posted Almost 13 years ago. Visible to the public.

In shell scripts you can use $1 to refer to the first argument, $2 for the second, etc. If you want to refer to all arguments (e.g. when writing a bash script that takes an arbitrary amount of arguments and passes them on to another call), you may not want to do a “$1 $2 $3 $4 ...”.

Use $@ instead, like in this script:
$ cat welcome
#!/bin/bash
echo Hello to $@

When called, the above would produce this:
$ ./welcome the world and universe
Hello to the world and universe

Note that $@ works for both sh and bash.

Arne Hartherz
Last edit
Almost 12 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-07-01 13:28)