$0 is script name or the command execuated
$# is the number of arguments
Positional parameters in a shell script are nothing but the command line arguments passed to a shell script. The following are some of the positional parameters used:
Let us see this with an example:
Similarly, the command line arguments can be accessed using $1, $2 till $9. However, if the number of command line arguments is more than 9, the same notation cannot be used. Instead, it should be used like ${10}, ${11} and so on. Let us see this with an example:
$# is the number of arguments
#! knows the shell
() executed in sub shell
{} executed in current shell only
$@ but with "" = $* = all parameters in single string
$? exit status of last command
$$ PID of current shell
$! PID of last background job
Positional parameters in a shell script are nothing but the command line arguments passed to a shell script. The following are some of the positional parameters used:
$# - Total number of arguments $0 - Command or the script name $1,$2, $3 - First, second and third args respectively. $* - All the command line arguments starting from $1.
Let us see this with an example:
[root@gpr ~]# cat cmd #!/usr/bin/ksh echo "The total no of args are: $#" echo "The script name is : $0" echo "The first argument is : $1" echo "The second argument is: $2" echo "The total argument list is: $*"Output:
[root@gpr ~]# ./cmd 1 2 3 4 The total no of args are: 4 The script name is : ./cmd The first argument is : 1 The second argument is: 2 The total argument list is: 1 2 3 4 [root@gpr ~]#As shown in the above output, $# printed 4 which is the total number of arguments, $0 printed the script name.
Similarly, the command line arguments can be accessed using $1, $2 till $9. However, if the number of command line arguments is more than 9, the same notation cannot be used. Instead, it should be used like ${10}, ${11} and so on. Let us see this with an example:
[root@gpr ~]# cat cmd #!/usr/bin/ksh echo "The total no of args are: $#" echo "The script name is : $0" echo "The first argument is : $1" echo "The second argument is: $2" echo "The incorrect 10th arg is : $10" echo "The correct 10th arg is : ${10}" [root@gpr ~]#Output:
[root@gpr ~]# ./cmd a b c d e f g h i j The total no of args are: 10 The script name is : ./cmd The first argument is : a The second argument is: b The incorrect 10th arg is : a0 The correct 10th arg is : j [root@gpr ~]#As shown above, the correct result appeared when we used ${10}. When $10 is used, the shell interprets it as $1 concatenated with 0, and hence you get the result as a0 ($1 is a). The same terminology will be used for all the arguments after the 9th.
No comments:
Post a Comment