Linux Version: CentOS 7

[root@azfdbdfsdf230lqdg1ba91 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@azfdbdfsdf230lqdg1ba91 ~]# uname -aLinux azfdbdfsdf230lqdg1ba91 3.10.0-693.2.2. El7. X86_64#1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@azfdbdfsdf230lqdg1ba91 ~]#
Copy the code

$symbol set of grass

The target

The $symbol has many important uses in Linux, especially when writing bash scripts. Because of its ever-changing and varied nature, it is challenging to master and use it, especially to memorize it. So, now, let’s make a summary of its usage to form a grass collection. Mastering them won’t get you a pay bump because they won’t be asked in interviews, but it will improve your productivity and broaden your horizons

$0 $1 $n, $#, $@ $*, $? ${{#}, ${#}, $[], $-, $! ,? . Additional! $,!!!!! “, in turn

$Get variable values

$gets the value of the variable

[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
Copy the code

"$"It is best to use “to get variable values

Why is this advice? Look at the example

[root@izbp10lqlgy2g31s41bt94z ~]# echo get value of a = $a
get value of a = 1
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = $a"
get value of a = 1
Copy the code

As you can see, “With or without” seems to have the same effect in double quotes. Don’t jump to conclusions. Read on

[root@izbp10lqlgy2g31s41bt94z ~]# a="i am skyler"
[root@izbp10lqlgy2g31s41bt94z ~]# [ $a == "i am skyler" ]-bash: [: too many parametersCopy the code

So let’s parse this[].[]It’s the conditional judgment symboltestCommand. What he means is whether the value of variable A is equal to"i am skyler". So why do I get an error? Because[ $a == "i am skyler"] this way the variable is parsed into[ i am skyler == "i am skyler" ]And, obviously, this is not going to be able to judge the strings on both sides of the equal sign, so what we want is[ "i am skyler" == "i am skyler" ]Comparison. So usually we arename”

[root@izbp10lqlgy2g31s41bt94z ~]# [ "$a" == "i am skyler" ]
[root@izbp10lqlgy2g31s41bt94z ~]# echo $?
0
Copy the code

I’m just going to use double quotes, so I’m going to use $? “, he means to determine whether the previous command execution result is correct. In the output result, 0 indicates that the execution is successful, and a non-zero value indicates that an error occurs

The ${}A boundary used to distinguish variables and explicitly tell the program which variable value to take

The following example, without {} program cannot determineAb $ABC which is a variable cannot be resolved

[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = $abc"
get value of a =
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = ${a}bc"
get value of a = 1bc
[root@izbp10lqlgy2g31s41bt94z ~]#
Copy the code

# ${}Gets the length of the variable value

[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length  of a = 1
[root@izbp10lqlgy2g31s41bt94z ~]# a=11111
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length  of a = 5
[root@izbp10lqlgy2g31s41bt94z ~]# a=skyler
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length  of a = 6
[root@izbp10lqlgy2g31s41bt94z ~]#
Copy the code

$0 $1 $nGets file names and parameter values. This parameter is common in bash scripts

$0 indicates the shell script file name; The number of arguments starts at 1, and 1 is the first argument. Here we create a test.sh executable file

Create a test.sh file and populate it with code [root@izbp10lqlgy2g31s41bt94z ~]# echo 'echo $0 $1 $2' > test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh
echo $0 The $1 $2Run test.sh and pass in the variable [root@izbp10lqlgy2g31s41bt94z ~]# sh test.sh i am skyler
test.sh i am
Copy the code

As you can see, the first two of the three arguments are printed, since we didn’t declare $3, so the file name and the first two arguments are printed

$#Get the number of parameters

[root@izbp10lqlgy2g31s41bt94z ~]# echo 'echo $# $0 $1' > test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh
echo $# $0 The $1
[root@izbp10lqlgy2g31s41bt94z ~]# sh test.sh I am a shuashua
4 test.sh I
Copy the code

$@ $*An array refers to a list of arguments

The difference is that when you put them in double quotation marks, if you pass in 1, 2, 3, then”The value of * is a variable of “1, 2, 3”

test.sh
echo '$@ array parameter format '
for x in "$@"
do
  echo + $x
done
echo 'Array argument format for $*'
for x in "$*"
do
  echo + $x
done

root@izbp10lqlgy2g31s41bt94z:~# sh test.sh 1 2 3
$@The array argument format is + 1 + 2 + 3 $*Copy the code

$?Check whether the previous command was executed successfully

Success is 0, failure is non-0

[root@izbp10lqlgy2g31s41bt94z ~]# ll172-rw-r --r-- 1 root root 49392 2月 25 2019 hs_err_pid24203.log -rw-r--r-- 1 root root 49425 2月 13 2019 hs_err_pid25726.log [root@izbp10lqlgy2g31s41bt94z ~]# echo $?
0
[root@izbp10lqlgy2g31s41bt94z ~]# ca ff-bash: ca: not found command [root@izbp10lqlgy2g31s41bt94z ~]# echo $?
127
Copy the code

The $()Equivalent to using double quotation marks

slightlyCopy the code

$[]Expression calculation

At this point, [] is not used in a deterministic scenario, as [] is generally used for conditional statements such as if while in bash footsteps

[root@izbp10lqlgy2g31s41bt94z ~]# echo $[5 + 5]
10
Copy the code

$-Displays the current options used by the shell

[root@izbp10lqlgy2g31s41bt94z ~]# echo $-HimBH is a shell option for each character of himBH, detail man bash then search -h-b etc. Details refer to: http://kodango.com/explain-shell-default-optionsCopy the code

$!Gets the pid of the last process to run in the background

[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh &
[1] 362
[root@izbp10lqlgy2g31s41bt94z ~]# echo $# $0 $1^C [1]+ complete cat test.sh [root@izbp10lqlgy2g31s41bt94z ~]# echo $!
362
Copy the code

! $Passing the parameters of the previous command to the parameters of the next command is more convenient and more commonly used in bash scripts

[root@izbp10lqlgy2g31s41bt94z ~]# cd /Users/skyler/project/test
[root@izbp10lqlgy2g31s41bt94z ~]# ll ! $
[root@izbp10lqlgy2g31s41bt94z ~]# ll /Users/skyler/project/test
362
Copy the code

!!!!!Print out the last command, which is more convenient to use in bash scripts

[root@izbp10lqlgy2g31s41bt94z ~]#!!!!!
[root@izbp10lqlgy2g31s41bt94z ~]# ll /Users/skyler/project/test
Copy the code

?Get the PID of the current process

[root@izbp10lqlgy2g31s41bt94z ~]# echo ?
31268
[root@izbp10lqlgy2g31s41bt94z ~]# ps -ef|grep 31268
root     31268 31266  0 08:10 pts/0    00:00:00 -bash
Copy the code

The current process is bash with pid 31268

Practice more on weekdays, when you use it

The $symbol is used in Linux