This article will explain what the $@ is in Bash and Bash/Shell scripting and how and why you might use it.
The $@ variable is a special variable in Bash which holds the value of all of the command line arguments/parameters passed to the script.
Command Line Arguments/Parameters in Bash
Using command-line arguments/parameters in shell scripts is an important and useful feature, so we have a full article on it:
This article follows on from our article on Command Line Arguments in Shell/Bash Scripts
$@ Contains All Parameters/Arguments Passed to the Script
The easiest way to illustrate how $@ works, is of course, with some example code.
The following example script simply prints out the contents of the variable $@
#!/bin/bash echo $@
Calling this script with some example arguments:
sh test.sh "apple" 0 banana 1
Will result in the following output:
apple 0 banana 1
Thus showing that the $@ variable simply holds the value of all of the parameters passed to the script.
Note that the quotes have been stripped, as it’s the value of the parameters which is contained in $@ rather than the text passed.
All positional parameters are included from position 1 (Position 0 contains the name of the script being executed and is omitted).
$@ is a special variable with limited use cases – it is recommended that you stick to using positional parameters and to use them as they are intended to be used (passing one value to one parameter) rather than trying to use $@ to read a heap of data into your script and trying to parse it yourself.