The positional parameters are the words following the name of a shell script. They are put into the variables $1, $2, $3 and so on. As long as needed, variables are added to an internal array. $# holds the total number of parameters, as is demonstrated with this simple script:
# This script reads 3 positional parameters and prints them out.
echo "$1 is the first positional parameter, \$1."
echo "$2 is the second positional parameter, \$2."
echo "$3 is the third positional parameter, \$3."
echo "The total number of positional parameters is $#."
Upon execution one could give any numbers of arguments:
franky ~> positional.sh one two three four five
one is the first positional parameter, $1.
two is the second positional parameter, $2.
three is the third positional parameter, $3.
The total number of positional parameters is 5.
kao i sa
A few special symbols and their meanings w.r.t shell scripts
$* - This denotes all the parameters passed to the script
at the time of its execution. Which includes $1, $2
and so on.
$0 - Name of the shell script being executed.
$# - Number of arguments specified in the command line.
$? - Exit status of the last command.
The above symbols are known as positional parameters. Let me explain the positional parameters with the aid of an example. Suppose I have a shell script called my_script.sh . Now I execute this script in the command line as follows :
$ ./my_script.sh linux is a robust OS
... as you can see above, I have passed 5 parameters to the script. In this scenario, the values of the positional parameters are as follows:
$* - will contain the values 'linux','is','a','robust','OS'.
$0 - will contain the value my_script.sh - the name of the script being
$# - contains the value 5 - the total number of parameters.
$$ - contains the process ID of the current shell. You can use this parameter while giving unique names to any temporary files that you create at the time of execution of the shell.
$1 - contains the value 'linux'
$2 - contains the value 'is'
... and so on.
P.S. trazio sam na reci "bash number of parameters", ovi su ti drugi i treci u listi.... stvarno ne znam kako ih nisi nasao :))
Kad sve ostalo zakaže, pročitaj uputstvo...