Home | Projects | Notes > Unix/Linux > Shell Script
Positional parameters: $0
, $1
, $2
, ...
These variables hold the values of command-line arguments passed to the script when it is executed.
$0
: Contains the name of the script or the command used to invoke the script. It represents the script's own name
$1
, $2
, $3
... : Hold the values of the command-line arguments passed to the script. $1
corresponds to the first argument, $2
corresponds to the second argument, and so on. The number after the $
indicates the position of the argument.
Example:
Consider the following script named "myscript.sh"
xxxxxxxxxx
51
2
3echo "Script name: $0"
4echo "First argument: $1"
5echo "Second argument: $2"
If you execute the script with command-line arguments like this:
xxxxxxxxxx
11$ ./myscript.sh arg1 arg2
The script will output:
xxxxxxxxxx
31Script name: ./myscript.sh
2First argument: arg1
3Second argument: arg2