Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the $symbol in linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to use the $symbol in linux. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Linux usage version: CentOS 7

[root@azfdbdfsdf230lqdg1ba91 ~] # cat / etc/redhat-releaseCentOS 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 GNU/Linux [root@azfdbdfsdf230lqdg1ba91 ~] #

$symbol draft collection

target

The $symbol plays a very important role in the linux system, especially when writing bash scripts, $can be seen everywhere. Because of his variety and variety, it brings challenges to his mastery and use, especially memory, so now, let's make a summary of its usage to form a grass collection. Mastering them will not give you a big raise in salary, because the interview will not ask, but it will improve your work efficiency and broaden your horizons.

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

$get the value of the variable

$can get the value of the variable

[root@izbp10lqlgy2g31s41bt94z ~] # axi1 [root@izbp10lqlgy2g31s41bt94z ~] # echo $A1

"$" is best used when getting the value of a variable.

Why is there such a suggestion? look at the example.

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

As you can see, the effect of double quotation marks seems to be the same. don't jump to conclusions, read on.

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

Parsing here [], [] is the conditional judgment symbol, which is equivalent to the test command. What he means is to determine whether the value of variable an is equal to "i am skyler".

So why is it wrong, because [$a = = "i am skyler"] is parsed to become [i am skyler = = "i am skyler"]. Obviously, this judgment cannot judge the strings on both sides of the equal sign, what we want is a comparison of ["i am skyler" = "i am skyler"]. So usually we are name. "

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

It is fine after using double quotation marks, where the next $? is used in advance, which means to determine whether the previous command is executed correctly. In the output result, 0 indicates successful execution, while a non-zero value indicates error

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

For the following example, it is impossible to determine which variable is ab $abc without adding {} program, which cannot be parsed.

[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 ~] #

${#} get the length of the variable value

[root@izbp10lqlgy2g31s41bt94z ~] # echo "get length of a = ${# a}" get length of aq1 [root@izbp10lqlgy2g31s41bt94z ~] # aq11111 [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 are usually found in bash scripts.

$0 represents the file name of the shell script; 1 represents the first parameter, and 1 represents the first parameter. Here we create a test.sh executable

Create a test.sh file and fill in the code [root@izbp10lqlgy2g31s41bt94z ~] # echo 'echo $0 $1 $2' > test.sh [root@izbp10lqlgy2g31s41bt94z ~] # cat test.shecho $0 $1 $2 execute test.sh and pass in the variable [root@izbp10lqlgy2g31s41bt94z ~] # sh test.sh I am skylertest.sh I am

As you can see, the first two of the three parameters are printed, because we didn't declare $3, and all printed out 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.shecho $# $0 $1 [root@izbp10lqlgy2g31s41bt94z ~] # sh test.sh I am a shuashua4 test.sh I

Reference parameter list in the form of $@ $* array

The difference between them is that when enclosed in double quotes, assuming that the passed parameter is 1 2 3, then the value of "*" is "1 2 3" a variable.

Test.shecho'$@ array parameter format 'for x in "$@" do echo + $xdoneecho' $* array parameter format 'for x in "$*" do echo + $xdoneroot@izbp10lqlgy2g31s41bt94z:~# sh test.sh 1 2 3 $@ array parameter format + 1 + 2 + 3 $* array parameter format + 1 2 3

$? Determine whether the execution of the last command was successful

Execution success value is 0, failure is non-0

Total amount of [root@izbp10lqlgy2g31s41bt94z] # ll: 1 root root 49392 February 25 2019 hs_err_pid24203.log-rw-r--r-- 1 root root 49425 February 13 2019 hs_err_ pid25726.log [root @ izbp10lqlgy2g31s41bt94z] # echo $? 0 [root@izbp10lqlgy2g31s41bt94z ~] # ca ff-bash: ca: command [root@izbp10lqlgy2g31s41bt94z] # echo $? 127 not found

$() is equivalent to the use of double quotation marks

Slightly

$[] expression evaluation

At this time, [] is not used for judgmental scenarios, but is generally used for conditional statements such as if while in bash footsteps.

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

$- displays the current options used by shell

[root@izbp10lqlgy2g31s41bt94z ~] # echo $- himBH explains: himBH each character is an option for shell, details man bash and then search for-h-B and so on. For more information, please see: http://kodango.com/explain-shell-default-options

$! Get the pid of the last process running in the background, and apply more to the bash script

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

! $pass the parameters of the previous command to the parameters of the next command, which is usually more convenient and more used in bash scripts

[root@izbp10lqlgy2g31s41bt94z ~] # cd / Users/skyler/project/test [root@izbp10lqlgy2g31s41bt94z ~] # ll! $[root@izbp10lqlgy2g31s41bt94z ~] # ll / Users/skyler/project/test362

!! The output of the previous command is usually more convenient and more applied to bash scripts.

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

$$gets the current process pid

[root@izbp10lqlgy2g31s41bt94z ~] # echo $31268 [root@izbp10lqlgy2g31s41bt94z ~] # ps-ef | grep 31268root 31268 31266 0 08:10 pts/0 00:00:00-bash this is the end of sharing about how the $symbol is used in linux. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report