This article will explain what the [email protected] is in Bash and Bash/Shell scripting and how and why you might use it.
The [email protected] 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
[email protected] Contains All Parameters/Arguments Passed to the Script
The easiest way to illustrate how [email protected] works, is of course, with some example code.
The following example script simply prints out the contents of the variable [email protected]
#!/bin/bash echo [email protected]
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 [email protected] 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 [email protected] 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).
[email protected] 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 [email protected] to read a heap of data into your script and trying to parse it yourself.