Bash is the
Shell that sits at the core of the Linux operating system and is responsible for processing commands via a text window.
Bash Shell has a lot of overlaps with the
Bourne Shell in terms of command syntax, but it has many advantages as well. While submitting a script in the command line, one can use both positional and special
parameters. While positional
parameters are passed by position, the latter is done by using
special settings. One can directly assign parameters by using the
'set' command. Commands like '
shift' allow users to shift the
parameters.
Parameters with Bash
Introduction
You can provide a script on the command line and arguments also known as parameters for its implementation.
There are two categories of parameters: positional and special.
Positional parameters only include all the arguments situated under "settings" on the command line, the invocation of a script.
They are then assigned to the variables reserved 1,2,3... 9,10,11... and are displayed by using the expressions $ 1, $ 2 ...${ 10), $ (11) ...
Note: The Bourne shell is limited to the parameters from 0 to 9 only.
Example 1
Below is an example of a small script for showing you some of the arguments passed as parameters by position.
#! / bin / bash
# Affiche_param.sh
echo "The 1st parameter is: $ 1"
echo "The 3rd parameter is: $ 3"
echo "The 10th parameter is: $ (10)"
echo "The 15th parameter is: $ (15)"
Simply invoke the script by passing a number of parameters:
. / display_param.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
The 1st parameter is: 1
The 3rd parameter is: 3
The 10th parameter is: 10
The 15th parameter is: 15
or:
. / display_param.sh one 2 three 4 5 6 7 8 9 ten 11 12 13 14 fifteen 16 17
The 1st parameter is a
The 3rd parameter is three
The 10th parameter is ten
The 15th parameter is fifteen
If certain parameters contain special characters or spaces, it
must then "quote":
. / display_param.sh a 2 "3rd" 4 5 6 7 8 9 ten 11 12 13 14 15th 16 17
The 1st parameter is a
The 3rd parameter is the 3rd
The 10th parameter is ten
The 15th parameter is the 15th
Special parameters are reserved variables that allow for some preferencial treatments on the parameters themselves.
These parameters are:
$ 0 Contains the name of the script as used
$ * All of the parameters in the form of a single argument
$ @ All arguments, an argument by setting
$ # The number of parameters passed to the script
$? The return code of last command
PID $ $ The su shell script that runs the
$! The PID of last process started in background
Example 2
Here's another little script implementing all special settings seen above:
#! / bin / bash
# display_param_2.sh
# Display script name su
echo "The name of my script is: $ 0"
# Display the number of parameters
echo "You spent $ # parameters"
# List of parameters (one argument)
for param in "$ *"
do
echo "The parameters (a single argument): $ param"
done
# List of parameters (one parameter per argument)
echo "The parameters (a parameter argument):"
for param in "$ @"
do
Echo-e "\ tParameter: $ param"
done
# Display process
echo "The PID of the shell running the script is: $ $"
# Execute command running in background
sleep 100 &
# Display of the process launched in the background
echo "The PID of the last command executed in the background is: $!"
# Display the return code of last command echo "
echo "return code from the preceding command is: $?"
# Generate an error
echo "Generating a mistake ..."
# Display the wrong order
echo "ls / etc / password 2> / dev / null"
ls / etc / password 2> / dev / null
# Display the return code of last command
echo "return code from the preceding command is: $?"
exit
What gives with the following invocation:
./display_param_2.sh 1 2 3 Four 5 Six
The name of my script is: ./display_param_2.sh
You've spent 6 parameters
The parameters (a single argument): 1 2 3 Four 5 Six
The parameters (a parameter argument):
Parameter: 1
Parameter: 2
Parameter: 3
Parameter: four
Parameter: 5
Setting: six
The PID of the shell running the script is: 6165
The PID of the last command executed in the background is: 6166
The return code from the preceding command is: 0
Generating a mistake ...
ls / etc / password 2> / dev / null
The return code from the preceding command is: 1
Initialise parameters
- The "set" --
It is possible to directly assign parameters to the shell with the command "set".
A simple command such as:
set param1 param2 param3
automatically initialises the positional parameters "
$ 1, $ 2, $ 3 with the values" param1, param2, param3 ", thus deleting the old values if they existed. Special parameters "#, * @ and" are automatically updated accordingly.
Examples
$ Set param1 param2 param3
$ Echo "Number of parameters: $ #"
Number of parameters: 3
$ Echo "The second parameter is: $ 2"
The second parameter is: param2
$ Echo "The parameters are: $ @"
The parameters are: param1 param2 param3
$ Set fishing apple
$ Echo "Number of parameters: $ #"
Number of parameters: 2
$ Echo "The parameters are: $ @"
The parameters are: fishing apple
This feature can be useful in file processing line
by line to isolate each word (field), and to format the output.
$ IFS =":"; set $ (grep $ USER / etc / passwd)
$ Echo-e "login: \ t $ 1 \ nNom: \ t $ 5 \ nID: \ t $ 3 \ ngroup: \ t $ 4 \ nShell: \ t $ 7"
Login: JM
Name: John Mackay
ID: 500
Group: 500
Shell: / bin / bash
- The "shift" --
The internal command "shift" as it allows to shift the parameters.
The value of 1st parameter ($ 1) is replaced by the value of the 2nd parameter ($ 2), the 2nd parameter ($ 2) by the 3rd parameter ($ 3), etc ...
It may be mentioned as an argument (shift n) the number of steps (position) to be offset settings.
Example 3
This is an implementation of the use of the internal shift.
#! / bin / bash
# Decale_param.sh
echo
echo "Number of parameters: $ #"
echo "The 1st parameter is: $ 1"
echo "The 3rd parameter is: $ 3"
echo "The 6th parameter is: $ 6"
echo "The 10th parameter is: $ (10)"
echo "============================================="
echo "Offset of a step with the \" shift \ ""
shift
echo "Number of parameters: $ #"
echo "The 1st parameter is: $ 1"
echo "The 3rd parameter is: $ 3"
echo "The 6th parameter is: $ 6"
echo "The 10th parameter is: $ (10)"
echo "============================================="
echo "Offset of four steps with the \" shift 4 ""
shift 4
echo "Number of parameters: $ #"
echo "The 1st parameter is: $ 1"
echo "The 3rd parameter is: $ 3"
echo "The 6th parameter is: $ 6"
echo "The 10th parameter is: $ (10)"
echo
And the result:
. / decal_param.sh 1 2 3 4 5 6 7 8 9 10
Number of parameters: 10
The 1st parameter is: 1
The 3rd parameter is: 3
The 6th parameter is: 6
The 10th parameter is: 10
=============================================
Lag a step with "shift"
Number of parameters 9
The 1st parameter is: 2
The 3rd parameter is: 4
The 6th parameter is: 7
The 10th parameter is:
=============================================
Lag of four steps with the command "shift 4"
Number of parameters: 5
The 1st parameter is: 6
The 3rd parameter is: 8
The 6th parameter is:
The 10th parameter is:
See also
Knowledge communities.
Published by
deri58 -
Latest update by Jeff