A. Variable list
Two. Experiments
System: CentOS 7
1. Special variables
[Email protected] ~_~ day2]# cat p.sh
#!/bin/bash
Echo ' $: ' $
Echo ' $*: ' $*
Echo ' [email protected]: ' [email protected]
Echo ' $#: ' $#
Echo ' $1$2$3: ' $1$2$3
[[email protected] ~_~ day2]# sh p.sh a b c D
$0:p.sh
$*:a b c D
[email protected]:a b c D
$#:4
$1$2$3:abc
2.$$
[Email protected]~~ day2]# Echo $$
1974
3.shift command: shift position parameter, rename positional parameter $N + 1, $N +2 ... to $ ... If no N is given, it is assumed to be 1. $ #会少N after each move
Exit Status: Returns success, such as n is negative or greater than $ #则失败
(1). Example one, move position variable
[Email protected] ~_~ day3]# cat r.sh
#!/bin/bash
Until [$#-eq 0]
Do
echo "The first value is:$1 the number of values is:$#"
Shift
Done
[[email protected] ~_~ day3]# sh r.sh 1 2 3 4 5
The first value is:1 the number of values Is:5
The first value is:2 the number of values Is:4
The first value is:3 the number of values Is:3
The first value is:4 the number of values Is:2
The first value is:5 the number of values Is:1
(2). Example two, using shift shift to accumulate
[Email protected] ~_~ day3]# cat s.sh
#!/bin/bash
Sum=0
Until [$#-eq 0]
Do
sum= ' expr $sum + $ '
Shift
Done
echo "sum is: $sum"
[[email protected] ~_~ day3]# sh s.sh 1 2 3 4 5
Sum is:15
Note: The above displacement is 1, the displacement is greater than 1 o'clock, the number of input parameters are different, there may be a logic error.
Shell Foundation--shell Special variables