This is the 26th day of my participation in the August More Text Challenge

A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.

Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!

Multiple uses of the ‘$’ symbol in Shell scripts

Various uses of the ‘$’ symbol in Shell scripts

In general, the following are the most commonly used items at work:

The $sign instructions
$0 The Shell command itself

1 to 1 to
9
Represents the number of arguments of the Shell
$? Displays the execution of the last command
$# The number of arguments passed to the script
$$ ID of the process in which the script is running
$* Displays all arguments passed to the script as a single string
$! The ID of the last process to run in the background
$- Displays the current options used by the Shell

Today we will pass the above options and make further operational cases;

1.1 Reference Variables

When referencing variables, use the $sign directly to reference them, and include loop variables;

[root@localhost ~]# x=1024
[root@localhost ~]# echo $x
1024
Copy the code

String enclosed by double quotation marks supports variable interpolation.

[root@localhost ~]# x=1024
[root@localhost ~]# echo "x = $x"
x = 1024
Copy the code

Use ${} for word boundaries.

[root@localhost ~]# x=1024
[root@localhost ~]# echo "x = ${x}xy"
x = 1024xy
Copy the code

Use ${#} to get the variable string length.

[root@localhost etc]# s=helloworld
[root@localhost etc]# echo "s.length = ${#s}"
s.length = 10
Copy the code

1.2 Referencing script or function parameters

Based on the way the script is referenced, 1 represents the Shell script file name, n starts at 2 for the NTH argument, and the second argument is $2.

[root@localhost ~]# echo 'echo $1 $2 $3' > ping.sh
[root@localhost ~]# cat ping.sh
echo $1 $2 $3
[root@localhost ~]# sh ping.sh 1 2 3
1 2 3
Copy the code

Strings enclosed in single quotes are not interpolated and use $# to get the number of script or function arguments;

[root@localhost ~]# echo 'echo $#' > ping.sh
[root@localhost ~]# sh ping.sh 1 2 3
3
Copy the code

1.3 Return value of the previous command

Using the $? The return value of the previous command.

0: indicates that there are no errors. Other values: indicates that there are errors.

[root@localhost ~]# true 1024
[root@localhost ~]# echo $?
0
[root@localhost ~]# false 2048
[root@localhost ~]# echo $?
1
Copy the code

1.4 Executing and obtaining the command output

Execute with $() and get the command output assigned to a variable, equal to double quotes.

[root@localhost ~]# echo 'date' Sunday, June 05, 2016 12:39:08 CST [root@localhost ~]# echo $(Date) Sunday, June 05, 2016 12:39:34 CSTCopy the code

1.5 Expression evaluation

[root@localhost ~]# echo $[1024 + 2048]
3072
[root@localhost ~]# expr 1024 + 2048
3072
[root@localhost ~]# a=1024
[root@localhost ~]# b=2048
[root@localhost ~]# echo $[ a + b ]
3072
Copy the code

1.6 Obtaining the CURRENT Process ID

Use $$to get the ID of the current process.

[root@localhost ~]# echo $$
55580
Copy the code

1.7 ID of the last process running in the background

Using the $! To get the ID of the last process running in the background.

Use & at the end of the command to create a background process.

[root@localhost ~]# tail -f /root/ping.sh & [2] 55848 [root@localhost ~]# echo $! 55848 [root@localhost ~]# kill $! [root@localhost ~]# echo $! 55848 [2]+ Tail -f /root/ping.shCopy the code

1.8 Obtaining Shell options

Use $- to get the current Shell option.

[root@localhost ~]# echo $-
himBH
Copy the code

Recommended reading

Super hardcore! 11 very useful Python and Shell scripts to use examples!

7 very useful Shell take to use script examples!

Shell programming – condition test | base paper

Shell programming – control structure | foundation

Shell programming | script parameters and the interaction and common problems

In this paper, to the end.


Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!

By the way, please give me some free attention! In case you get lost and don’t find me next time.

See you next time!