Script arguments¶
Overview¶
Arguments of a script The request to enter information with the read command interrupts the execution of the script as long as the user does not enter any information.
This method, although very user-friendly, has its limits if the script is scheduled to run at night. To overcome this problem, it is possible to inject the desired information via arguments.
Many Linux commands work on this principle.
This way of doing things has the advantage that once the script is executed, it will not need any human intervention to finish.
Its major disadvantage is that the user will have to be warned about the syntax of the script to avoid errors.
The arguments are filled in when the script command is entered. They are separated by a space.
./script argument1 argument2 Once executed, the script saves the entered arguments in predefined variables: positional variables.
These variables can be used in the script like any other variable, except that they cannot be assigned.
Unused positional variables exist but are empty. Positional variables are always defined in the same way: Variable Observation $0 contains the name of the script as entered. $1 to $9 contain the values of the 1st to 9th argument ${x} contains the value of the argument x, greater than 9. $# contains the number of arguments passed. $* or $@ contains in one variable all the arguments passed. Example:
!/usr/bin/env bash¶
¶
Author : Damien dit LeDub¶
Date : september 2019¶
Version 1.0.0 : Display the value of the positional arguments¶
From 1 to 3¶
The field separator will be "," or space¶
Important to see the difference in $* and $@¶
IFS=", "
Display a text on the screen:¶
echo "The number of arguments ($#) = $#" echo "The name of the script ($0) = $0" echo "The 1st argument ($1) = $1" echo "The 2nd argument ($2) = $2" echo "The 3rd argument ($3) = $3" echo "All separated by IFS ($) = $" echo "All without separation ($@) = $@" This will give:
$ ./arguments.sh one two "tree four" The number of arguments (\(#) = 3 The name of the script (\)0) = ./arguments.sh The 1st argument (\(1) = one The 2nd argument (\)2) = two The 3rd argument (\(3) = tree four All separated by IFS (\)*) = one,two,tree four All without separation ($@) = one two tree four Warning
Beware of the difference between $@ and $*. It is in the argument storage format:
\(* : Contains the arguments in the format "\)1 \(2 (3 ..." \(@ : Contains arguments in the format "\)1" "\)2" "\)3" ... It is by modifying the IFS environment variable that the difference is visible.
The shift command¶ The shift command allows you to shift positional variables.
Let's modify our previous example to illustrate the impact of the shift command on positional variables:
!/usr/bin/env bash¶
¶
Author : Damien dit LeDub¶
Date : september 2019¶
Version 1.0.0 : Display the value of the positional arguments¶
From 1 to 3¶
The field separator will be "," or space¶
Important to see the difference in $* and $@¶
IFS=", "
Display a text on the screen:¶
echo "The number of arguments ($#) = $#" echo "The 1st argument ($1) = $1" echo "The 2nd argument ($2) = $2" echo "The 3rd argument ($3) = $3" echo "All separated by IFS ($) = $" echo "All without separation ($@) = $@"
shift 2 echo "" echo "-------- SHIFT 2 ----------------" echo ""
echo "The number of arguments ($#) = $#" echo "The 1st argument ($1) = $1" echo "The 2nd argument ($2) = $2" echo "The 3rd argument ($3) = $3" echo "All separated by IFS ($) = $" echo "All without separation ($@) = $@" This will give:
./arguments.sh one two "tree four" The number of arguments (\(#) = 3 The 1st argument (\)1) = one The 2nd argument (\(2) = two The 3rd argument (\)3) = tree four All separated by IFS (\(\*) = one,two,tree four All without separation (\)@) = one two tree four
-------- SHIFT 2 ----------------
The number of arguments (\(#) = 1 The 1st argument (\)1) = tree four The 2nd argument (\(2) = The 3rd argument (\)3) = All separated by IFS (\(\*) = tree four All without separation (\)@) = tree four As you can see, the shift command has shifted the place of the arguments "to the left", removing the first 2.