- Learning Linux Shell Scripting
- Ganesh Naik
- 353字
- 2021-06-25 22:02:56
Understanding set
Many times, we may not pass arguments to the command line, but we may need to set parameters internally inside the script.
We can declare parameters with the set command, as follows:
$ set USA Canada UK France$ echo $1USA$ echo $2Canada$ echo $3UK$ echo $4France
We can use this inside the set_01.sh script, as follows:
#!/bin/bash set USA Canada UK France echo $1 echo $2 echo $3 echo $4
Run the script as this:
$ ./set.sh
The output is as follows:
USACanadaUKFrance
Following is a summary of the declare options:

Type in the following commands:
set One Two Three Four Five
echo $0 # This will show command
echo $1 # This will show first parameter
echo $2echo $* # This will list all parameters
echo $# # This will list total number of parameters
echo ${10} ${11} # Use this syntax for parameters for 10th and # 11th parameters
Let's write script set_02.sh, as follows:
#!/bin/bash echo The date is $(date) set $(date) echo The month is $2 exit 0
The output is as follows:
In the script $(date), the command will execute, and the output of that command will be used as $1, $2, $3, and so on. We have used $2 to extract the month from the output.
Let's write script set_03.sh, as follows:
#!/bin/bash echo "Executing script $0" echo $1 $2 $3 set eins zwei drei echo "One two three in German are:" echo "$1" echo "$2" echo "$3" textline="name phone address birthdate salary" set $textline echo "$*" echo 'At this time $1 = '$1' and $4 = '$4''
The output is as follows:
Executing script ./hello.sh One two three in German are: eins zwei drei name phone address birthdate salary At this time $1 = name and $4 = birthdate
In this script, the output shows:
- Initially, when the set is not called, then $1, $2, and $3 do not contain any information.
- Then, we set $1 to $3 as GERMAN numerals in words.
- Then, we set $1 to $5 as the name, phone number, address, date of birth, and salary, respectively.
推薦閱讀