Linux variables have the prefix $. Variables that will be useful in shell scripting are listed below.
$$ Process ID number of the shell in execution
$? Variable indicating Exit status (from the last command that got executed)
$* Entire argument string in the command line (excluding script name)
$# Number of arguments in the command line (not counting the shell script name)
To access arguments passed to a script, following variables are used.
$0 Name of the program (with entire path)
$1 First argument passed in the command line
$2 Second argument passed in the command line
$n Nth argument passed in the command line
To move or shift to a specified argument in the list of arguments, “shift” command is used. For example, to shift 1 argument just type “shift” or “shift 1” and to shift to the third argument in the list type “shift 2“.
Program: helloargs.sh
#!/bin/bash
echo “Process ID number = $$”
echo “Program name = $0”
echo “Argument string = $*”
echo “Total arguments is $#”
echo “Argument 1 has the value $1”
shift 2
echo “Now argument 1 has the value $1”
Program Results
$ helloargs.sh arg1 arg2 arg3 arg4 Process ID number = 28141 Program name = /tmp/myshells/helloargs.sh Argument string = arg1 arg2 arg3 arg4 Total arguments is 4 Argument 1 has the value arg1 Now argument 1 has the value arg3