Shell Variable Knowledge Advancement

Posted by matthewd on Sun, 15 Sep 2019 09:12:15 +0200

I. Special and Important Variables in the shell

1. Special Location Variables

There are some special and important variables in the shell, such as: $0, $1, $#, we can call them special location variables. To pass parameters from the command line, function, or script execution, you need to use location parameter variables in shell scripts.

Commonly used special position parameter variables, as shown in Figure 1:

Examples are given to illustrate:
$0

[root@localhost ~]# cat a.sh
echo $0
[root@localhost ~]# sh a.sh
a.sh
[root@localhost ~]# sh /root/a.sh
/root/a.sh
[root@localhost ~]# dirname /root/a.sh
/root
//Output path name only
[root@localhost ~]# basename /root/a.sh
a.sh
//Output script name only

$n

[root@localhost ~]# cat a.sh
echo $1 $2 $3 $4 $5
[root@localhost ~]# sh a.sh a b c d e f
a b c d e
[root@localhost ~]# cat a.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[root@localhost ~]# sh a.sh {a..z}
a b c d e f g h i a0 a1 a2 a3 a4 a5
//The output of the tenth position parameter is abnormal.
[root@localhost ~]# cat a.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
[root@localhost ~]# sh a.sh {a..z}
a b c d e f g h i j k l m n o
//This is normal. The position variable is greater than 9. The number must be enclosed in "{}".

$#

[root@localhost ~]# cat a.sh
echo $#
[root@localhost ~]# sh a.sh a b c d e f
6

**$***

[root@localhost ~]# cat a.sh
echo $*
[root@localhost ~]# sh a.sh "a b" c d e
a b c d e

$@

[root@localhost ~]# cat a.sh
echo $@
[root@localhost ~]# sh a.sh "a b" c d e
a b c d e f

The output of "$*" without quotation marks is the same as that of "$@".

*"$"**

[root@localhost ~]# cat a.sh
for i in "$*";do echo $i;done
[root@localhost ~]# sh a.sh "a b" c d e
a b c d e

"$@"

[root@localhost ~]# cat a.sh
for i in "$@";do echo $i;done
[root@localhost ~]# sh a.sh "a b" c d e
a b
c
d
e

2. Special State Variables

As shown in the picture:

Examples are given to illustrate:
$?

[root@localhost ~]# pwd
/root
[root@localhost ~]# echo $?
0
[root@localhost ~]# fsdafasdf
bash: fsdafasdf: No command found...
[root@localhost ~]# echo $?
127
//The return value of executing the correct command is 0, and the incorrect return value is 0.
[root@localhost ~]# cat a.sh
if  
    [ $# -ne 2 ] 
then 
    echo "cuowu!"
    exit 123              //When the script is executed incorrectly, the value of $? Is 123.
fi
[root@localhost ~]# sh a.sh
cuowu!
[root@localhost ~]# echo $?
123

This command is very useful for beginners!

"$?" The use of the return value:

  • Determine whether commands, scripts or functions are successfully executed.
  • If the execution "exit number" is called in the script, the number is returned to the "$?" variable.
  • If it is in a function, the number is passed to "$" in the form of function return value by "return number". Variables;

The rest about'$','$!','$'are not very common, nor are they illustrated here!

2. shell built-in variable commands

The shell contains built-in commands that are not visible in the directory list and are provided by the shell itself. Common internal commands are echo, eval, exec, export, read, shift. Next, let's look at the functions of these commands.

echo command
Display the string and variables specified after the echo command to standard output.

The commonly used parameters of echo are as follows:

Examples are given to illustrate:

[root@localhost ~]# echo one;echo two
one
two
[root@localhost ~]# echo -n one;echo two
onetwo
//Non-newline output
[root@localhost ~]# echo -e "one\ttwo\nthree\tfour"
one two
three   four
//The gap between them is the function of "t", and "n" line change.
[root@localhost ~]# echo -e "1\b23"
23
//The function of b is backspace, so it blocks 1.

The echo command is very common, and it's not difficult, so that's all!

The eval command is not very common, and I have not used it very much, so I will not make an axe in class!

Exc command:

The exec command can turn to execute the specified command without creating a new subprocess. When the specified command is executed, the process (that is, the original Shell) terminates. An example is as follows:

[root@localhost ~]# exec date
Sun Sep 15 12:05:22 CST 2019
[lisi@localhost ~]$
//Back to privilege mode

read command:
Information such as strings is input from the standard and passed to variables defined within the Shell program.

shift command:
If the position variables are $1, $2, $3, then after a shift, $3 becomes $2, $2 becomes $1, and $1 disappears.

3. shell variable string

Examples of commonly used options are:

(1) Query character length values

Examples are given to illustrate:
Define variables:

[root@localhost ~]# a=abcdef
[root@localhost ~]# echo ${a}
abcdef
[root@localhost ~]# echo $a
abcdef

Returns the length of the variable value

[root@localhost ~]# echo ${#a}
6

Extension:

[root@localhost ~]# echo $a | wc -L
6
//Statistical Length Using wc Command
[root@localhost ~]# expr length "$a"
6
//Calculation with expr's length function
[root@localhost ~]# echo "$a" | awk '{print length($0)}'
6
//Using awk's length function to calculate variable length

The fastest way to do this is to use ${# variable name}.

(2) Interception character length

Define variables:

[root@localhost ~]# a="a b c d e f"
[root@localhost ~]# echo ${a}
a b c d e f

Intercept the contents of variables, starting with the second character (default to last)

[root@localhost ~]# echo ${a:2}
b c d e f

Intercept the contents of a variable, starting with the second character and two characters
The first method is:

[root@localhost ~]# echo ${a:2:4}
b c
//Because spaces are also characters.

The second method:

[root@localhost ~]# echo ${a} | cut -c 3-5
b c

(3) Delete specified characters

Define variables:

[root@localhost ~]# a=abcABC123ABCabc
[root@localhost ~]# echo $a
abcABC123ABCabc
[root@localhost ~]# echo ${a#a*c}
ABC123ABCabc
[root@localhost ~]# echo ${a#a*C}
123ABCabc
//Delete the shortest strings that match a*c and a*C from the beginning of the variable content
[root@localhost ~]# echo ${a##a*c}

//Because they all meet the requirements, they are not shown.
[root@localhost ~]# echo ${a##a*C}
abc
//Delete the longest strings that match a*c and a*C from the beginning of the variable content
[root@localhost ~]# echo ${a%a*c}
abcABC123ABC
[root@localhost ~]# echo ${a%a*C}
abcABC123ABCabc
//Delete the shortest strings that match a*c and a*C from the end of the variable content
[root@localhost ~]# echo ${a%%a*c}

[root@localhost ~]# echo ${a%%a*C}
abcABC123ABCabc
//Delete the longest string matching a*c and a*C from the end of the variable content

Summary of character deletion:

  • # The shortest match is deleted from the beginning.
  • ## The longest match is deleted from the beginning.
  • % The shortest match is deleted from the end.
  • %% The longest match is deleted from the end.

A C matched char ac ters, all, beginning with a and ending with c;
The characters matched by aC represent all, beginning with a and ending with C.

(4) Replace specified characters

Define variables:

[root@localhost ~]# a="abc abc"
[root@localhost ~]# echo $a
abc abc
[root@localhost ~]# echo ${a/abc/ABC}
ABC abc
//Replace the first character that meets the requirement
[root@localhost ~]# echo ${a//abc/ABC}
ABC ABC
//Replace all required characters

Summary of substitution:

  • A "/" represents the first string to replace the match.
  • Two "//" denotes the replacement of all matched strings;

IV. Special Extended Variables

Common options, as shown below:


Not very often, here is no example to illustrate!

Topics: Linux shell REST