Explain the usage of “$” in shell script


In general, the most commonly used items in work are as follows:

  • $0: Shell’s command itself
  • 1 to 9: represents the shell‘s first parameter
  • $?: displays the execution of the last command
  • $#: the number of arguments passed to the script
  • $$: the current process ID number of the script running
  • $*: displays all parameters passed to the script in a single string
  • $!: ID number of the last process running in the background
  • $-: displays the current options used by the shell
  • ……

Today, we will go through the above options and do further operation cases;

1. Reference variable

When referring to variables, use the $symbol to reference directly, and include loop variables;

[[email protected] ~]# x=1024
[[email protected] ~]# echo $x

The string enclosed by double quotation marks supports variable interpolation.

[[email protected] ~]# x=1024
[[email protected] ~]# echo "x = $x"
x = 1024

Use ${} as the word boundary.

[[email protected] ~]# x=1024
[[email protected] ~]# echo "x = ${x}xy"
x = 1024xy

Use ${} to get the variable string length.

[[email protected] etc]# s=helloworld
[[email protected] etc]# echo "s.length = ${#s}"
s.length = 10

2. Reference script or function parameters

Based on the method of reference script, 1 represents the file name of shell script, n starting from 2 represents the nth parameter, and the second parameter is $2;

[[email protected] ~]# echo 'echo $1 $2 $3' > ping.sh
[[email protected] ~]# cat ping.sh
echo $1 $2 $3
[[email protected] ~]# sh ping.sh 1 2 3
1 2 3

The string enclosed in single quotation mark ” will not be interpolated, and $ා is used to get the number of script or function parameters;

[[email protected] ~]# echo 'echo $#' > ping.sh
[[email protected] ~]# sh ping.sh 1 2 3

3. The return value of the previous command

Use the return value of the previous command in $.

0: no error, any other value: error.

[[email protected] ~]# true 1024
[[email protected] ~]# echo $?
[[email protected] ~]# false 2048
[[email protected] ~]# echo $?

4. Execute and get command output

Use $() to execute and get the command output assigned to a variable, equal to the function of double quotation marks.

[[email protected] ~]# echo `date`
Sunday, June 5, 2016 12:39:08 CST
[[email protected] ~]# echo $(date)
Sunday, June 5, 2016 12:39:34 CST

5. Expression evaluation

The expression is evaluated by using [], which is different from the command expr in that: [] is used for interpolation, expr is used to output the value.

[[email protected] ~]# echo $[1024 + 2048]
[[email protected] ~]# expr 1024 + 2048
[[email protected] ~]# a=1024
[[email protected] ~]# b=2048
[[email protected] ~]# echo $[ a + b ]

6. Get current process ID

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

[[email protected] ~]# echo $$

7. ID of the last process running in the background

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

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

Execute the command kill! And then enter echo! To terminate the ping.sh script.

[[email protected] ~]# tail -f /root/ping.sh &
[2] 55848
[[email protected] ~]# echo $!
[[email protected] ~]# kill $!
[[email protected] ~]# echo $!
[2] + tail - F / root terminated/ ping.sh

8. Get shell options

Use $- to get the current shell option.

[[email protected] ~]# echo $-

By Jack Tian
source: https://www.cnblogs.com/jacktian-it/

This article on the detailed explanation of the shell script ‘$’ symbol of a variety of uses to introduce this, more related shell $usage content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!

Recommended Today

How to share queues with hypertools 2.5

Share queue with swote To realize asynchronous IO between processes, the general idea is to use redis queue. Based on the development of swote, the queue can also be realized through high-performance shared memory table. Copy the code from the HTTP tutorial on swoole’s official website, and configure four worker processes to simulate multiple producers […]