Summary of Basic Usage of $Symbol in linux

  • 2021-07-09 09:41:13
  • OfStack

linux Usage Version: CentOS 7


[root@azfdbdfsdf230lqdg1ba91 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@azfdbdfsdf230lqdg1ba91 ~]# uname -a
Linux 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 ~]#

Symbolic grass set

Objectives

The $symbol plays a very important role in the linux system, especially when writing bash scripts, the figure of $can be seen everywhere. Because of his ever-changing and varied, it brings challenges to master and use him, especially memory. Therefore, now, let's summarize its usage once to form a grass collection. Mastering them won't make your salary jump because you won't be asked in the interview, but it will improve your work efficiency and broaden your horizons

At present, the usage of $I know is $, "$", $0 $1 $n, $#, $@ $*, $? , $(), ${}, ${#}, $[], $-, $! , $$. Extra! $,! ! , described in detail in turn

Gets the value of the variable

The value of the variable can be obtained


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

It is best to use "enclosed" when "$" gets the value of a variable

Why is there such a suggestion? 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

As you can see, with or without "double quotation marks, it seems that the effect is 1. Don't jump to conclusions, look down


[root@izbp10lqlgy2g31s41bt94z ~]# a="i am skyler"
[root@izbp10lqlgy2g31s41bt94z ~]# [ $a == "i am skyler" ]
-bash: [:  Too many parameters 

Here, [] is parsed, and [] is a conditional judgment symbol, which is equivalent to test command. What he means is to determine whether the value of the a variable is equal to "i am skyler".
So why do you report an error? Because the writing variable [$a = = "i am skyler"] is parsed into [i am skyler = = "i am skyler"]. Obviously, this judgment formula cannot judge the strings on both sides of the equal sign. What we want is ["i am skyler" = = "i ES60skyler"] So we are usually name. "


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

After using double quotation marks, it will be fine. Here, $is used in advance? He means to judge whether the execution result of the first command is correct. In the output result, 0 indicates successful execution, and non-zero value indicates error

${} is used to distinguish the boundaries of variables and explicitly tell the program which variable to take

In the following example, it is impossible to judge which variable is ab $abc without adding {} program and 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 ~]#

${#} 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 ~]#

$0 $1 $n gets the file name and parameter values, which is more common in bash scripts

$0 represents the shell script file name; Beginning with 1 indicates which parameter, and 1 indicates the first parameter. Here we create an test. sh executable


 Create 1 A test.sh File and populate the code 
[root@izbp10lqlgy2g31s41bt94z ~]# echo 'echo $0 $1 $2' > test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh
echo $0 $1 $2

 Execute test.sh  And pass in variables 
[root@izbp10lqlgy2g31s41bt94z ~]# sh test.sh i am skyler
test.sh i am

As you can see, the first two of the three parameters are printed out, because we didn't declare $3, so all of them are printed out with the file name and the first two parameters

$# Get the number of parameters


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

Formal reference parameter list of $@ $* array

The difference between them is that when enclosed in double quotation marks, assuming the parameter passed in is 1 2 3, the value of "*" is "1 2 3" 1 variable


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

root@izbp10lqlgy2g31s41bt94z:~# sh test.sh 1 2 3
$@ Array parameter format of 
+ 1
+ 2
+ 3
$* Array parameter format of 
+ 1 2 3

$? Determine whether the last command was executed successfully

0 when executing success value, failure is non-0


[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
0

$() is equivalent to using double quotation marks

Slightly

$[] Expression evaluation

At this time, [] is not used in the scenario of judgment formula, and [] is used in conditional statements such as if while in the step of bash as judgment formula 1


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

$-Displays the current options used by shell


[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
2

$! Get the pid of the last process running in the background, and more applications are in bash scripts


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

! Pass the parameters of the first command to the parameters of the next command, which is more convenient at ordinary times and more applied in bash scripts


[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
4

! ! Output the above command, which is usually used for more convenience and more applications in bash scripts


[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
5

$$Get the current process pid


[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1
6

The current process is bash, and pid is 31268

Practice more on weekdays and ask for it at will

Segment paging can also achieve the effect, which depends on the business scenario.

Summarize


Related articles: