In bash, we have many variables. Before learning what are positional parameters and special variables we should know what are variables and how to pass them into a shell script?
What are variables in Shell scripting?
A variable is a value holder which we can change when it is required. We can use these variables in our shell scripts so that we no need to hard code the values with the shell script.
- System variables
- User defined variables
- Special variables
- Positional parameters
- Special variables
We already discussed System variables, Some special variables, and command exit status in our other posts. Before learning positional we should know how we pass variables to a shell script.
How to pass variables in Shell scripting?
We can pass variables into shell scripts in different ways to avoid hard coding of values. We have different ways where we can pass variables to a shell script. Positional parameters are one kind of passing variables into shell scripting. Below are the way we can pass variables into shell scripting depending on what time you want to send them to a script.
- Within shell script(Variables defined with the script)
- Before start of shell script(Positional parameters)
- At the time of executing a shell script(using read command)
What are positional parameters?
cp test/ bash/
- $0: contains the name of script as it is invoked
- $1, $2, …, $n: indicates the position of the argument also called as positional parameters.
- $*: contains all the arguments regrouped as one argument
- $@: contains all the arguments, one argument per parameter
- $#: contains the number of parameters passed to the script
- $? : contains the return code of the previous command (it equaled 0 when the last command was executed successfully)
- $$: contains the PID of shell executing the script
- $! : contains the PID of last background process
- $_ : Last argument of an executed command
Examples:
Let’s create the script test.sh like below:
#/bin/bash function myFunction () { echo -e "--------------------------------------------" echo -e "Name of the script: $0" echo -e "--------------------------------------------" echo -e "the arguments regrouped as one argument: $*" echo -e "--------------------------------------------" echo -e "the arguments, one argument per parameter: $@" echo -e "--------------------------------------------" echo -e "the number of parameters: $#" echo -e "--------------------------------------------" echo -e "the first arg: $1" echo -e "the second arg: $2" echo -e "the third arg: $3" echo -e "--------------------------------------------" echo -e "the return code: $?" echo -e "--------------------------------------------" echo -e "the PID of shell executing the script: $$" echo -e "--------------------------------------------" echo -e "the PID of last background process: $!" echo -e "--------------------------------------------" } # Launch a background process # & is used to launch a process in background sleep 5 & # Call the function myFunction $1 $2 $3
Let’s run the script
user@server:~ $ bash test.sh arg1 arg2 arg3 -------------------------------------------- Name of the script: test.sh -------------------------------------------- the arguments regrouped as one argument: arg1 arg2 arg3 -------------------------------------------- the arguments, one argument per parameter: arg1 arg2 arg3 -------------------------------------------- the number of parameters: 3 -------------------------------------------- the first arg: arg1 the second arg: arg2 the third arg: arg3 -------------------------------------------- the return code: 0 -------------------------------------------- the PID of shell executing the script: 32117 -------------------------------------------- the PID of last background process: 32118 --------------------------------------------
How many positional parameters are there?
Positional parameters in Linux/Unix
Batch 1: $1, $2, $3, $4, $5, $6, $7, $8, $9
#!/bin/bash echo "My first positional parameter is $1" echo "My 10th positional parameter is $10"
bash abc.sh 24 surendra abc xyz red 12 as face 89 kumar
My first positional parameter is 24 My 10th positional parameter is 240
#!/bin/bash echo "My first positional parameter is $1" echo "My 10th positional parameter is ${10}"
bash abc.sh 24 surendra abc xyz red 12 as face 89 kumar
My first positional parameter is 24 My 10th positional parameter is kumar
Difference between $@ and $*
Example 1:for i in $@
do
echo $i
Done
for i Do Echo $i Done
I hope that this blog helped you. Please visit our website for other interesting blogs and feel free to leave your feedbacks and thoughts. Till next time!
Latest posts by ZIADI Mohamed Ali (see all)
- How to show mounted devices in Linux? - July 25, 2017
- How to use Positional parameters and special variables in Linux - June 28, 2017
- Linux: Connect to your WiFi network through CLI? - June 25, 2017
- How to find a file in Linux? - March 19, 2017
- Mysql: How to find table and database size? - January 9, 2017