The Unix "parameters", $0, $#, $1, $2, $3, ..., $?, $@

iKnow-IT banner

Parameters

$0 => Command-name
$# => contains the number of parameters for this shell is started
$1 $2 ... => parameter 1 parameter 2 ...
$? => check the return code
$@ => includes all the parameters that this shell is started


$0 => command-name

$ cat test.sh
echo "This is : "$0

$ ./test.sh
This is : test.sh

$

$# => contains the number of parameters for this shell is started

$ cat test2.sh
echo The number of parameters : $#

$ ./test2.sh 1 2 3 4 
The number of parameters : 4 

$ ./test2.sh a b 
The number of parameters : 2

 

$1 $2 … => parameter 1 parameter 2 …

$ cat test3.sh
echo $1 $2 $3 
shift 
echo $1 $2 $3 

$ ./test3.sh 1 2 3 4
1 2 3
2 3 4

$? => check the return code 
$ date 
Thu Apr 15 17:12:05 DFT 1999 
$ echo $? 


$ dte 
ksh: dte: not found 
$ echo $? 
127 

$@ => includes all the parameters that this shell is started 

$ Makenewusers piet /home/piet 
$echo $@ 
piet /home/piet 
$