In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the bash back quotation marks and $() how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you will gain after reading this bash back quotation marks and $() how to use the article, let's take a look at it.
In bash, both (backquotation marks) are used for command substitution, and command substitution is similar to variable substitution, both are used to reorganize the command line, first complete the command line in quotation marks, and then replace the results, and then re-form a new command line.
The use of backquotation marks
Here is a simple example:
[root@localhost ~] # echo "There are `ls | wc-l` files in this directory" There are 10 files in this directory
Ls | wc-l is used to list and count the number of files in the current directory, and then embed it into the echo command.
In the shell script, of course, you can do the same, assigning the result of the ls | wc-l command to a variable, which you will use later.
[root@localhost ~] # file_count= `ls | wc-l` [root@localhost ~] # echo "There are $file_count files in this directory" There are 10 files in this directory
How to use $()
You can also get the same result by using $() instead of "`backquotes, as shown in the following example:
[root@localhost ~] # echo "There are $(ls | wc-l) files in this directory" There are 10 files in this directory
Here is an example where I need to troubleshoot some problems with network connections, so I decided to display the total number of connections and the number of waiting connections per minute.
[root@localhost ~] # vim netinfo.shangxinfo.shangxinxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > netinfo.txt connections_total=$ (cat netinfo.txt | wc-l) connections_waiting=$ (grep WAIT netinfo.txt | wc-l) printf "$(date +% R)-Total=%6d Waiting=%6d\ n" $connections_total $connections_waiting sleep 60done
Run the script:
[root@localhost] #. / netinfo.sh17:13-Total= 158 Waiting= 417 Waiting= 14-Total= 162 Waiting= 017 Waiting= 15-Total= 155 Waiting= 017 Waiting= 16-Total= 155 Waiting= 017 Waiting= 0
How to choose which way to use
The $() method is more recommended here. Here are the reasons:\ 1. Operators can become confusing if internal commands are also used.
You will need to escape the internal "`, and it may be difficult to read and troubleshoot scripts if you use single quotes as part of the command or as part of the result. if you start thinking about nesting operators in other operators, things won't work as expected or won't work at all.
\ 2. The $() operator is safer and more predictable.
The content in the $() operator is treated as a shell script. Syntactically, this is the same as saving code in a text file.
Here are some examples of the differences in behavior between `and $():
[root@localhost ~] # echo'\ $x'\ $x [root@localhost ~] # echo `root'\ $x'` $x [root @ localhost ~] # echo $(root'\ $x')\ $x this is the end of the article on "how to use backquotes and $() in bash". Thank you for reading! I believe you have a certain understanding of the knowledge of "backquotes in bash and how to use $()". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.