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

Introduction to the usage of several parentheses and quotation marks in Linux Shell

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "introduction to the use of several parentheses and quotation marks in Linux Shell". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Shell scripts often need to use some parentheses, quotation mark expressions, different functions, this article describes in detail.

1. Double quotation marks ""

Double quotation marks are often used to contain a set of strings in which other characters (such as IFS, line feeds, carriage returns, etc.) have no special meaning except for "$", "\", and "`(back quotation marks)".

$axi3 $echo "$a"

The output is 3, and the $symbol still has a special meaning in double quotes.

2. Single quotation marks''

Single quotation marks function like double quotes, except that all characters in single quotation marks have no special meaning:

$axi3 $echo'$a'

The output is $a, which shows that the $symbol does not work in single quotation marks.

3. Backquotes ``

The function of the backquote is to replace the command. The content in the backquote ``is usually the command line. The program will first execute the content in the backquote and replace the content in the backquote with the running result. For example:

$echo `date`

This command first executes the command date in backquotes, and then uses the echo command to print out the result of the date command (the same effect as using the date command directly). Another example:

#! / bin/bash astat3 baked 5 c = `expr $a\ * $b` echo $c exit 0

4. $+ parentheses $()

$(.) Like backquotes, it is also a command replacement:

#! / bin/bash for file in $(ls /) do echo $file done exit 0

This script uses the for loop to print the file names of all the files in the root directory, uses $(ls /) to get all the files in the root directory, and passes them to the for structure as a parameter list.

5. $+ double parentheses $()

The function of $(()) is to perform arithmetic operations, the contents in parentheses are mathematical expressions, and you can use $() to find the value of mathematical expressions:

#! / bin/bash astat3 baked 5 cased $(($a * $b)) echo $c exit 0

The output of the above script is 15.

When using $(()) for mathematical operations, there is no need to worry about operators such as the multiplication sign (*) being misunderstood by shell as other meanings, because they are all in parentheses.

6. $+ brackets $[]

The function of $[] is the same as that of $(), which is used for arithmetic operations.

7. $+ braces ${}

The function of ${} is variable substitution, which is similar to the $symbol, but the replacement range of ${} is more precise than that of $:

#! / bin/bash astat3 baked 5 echo $ab echo ${a} b exit 0

In this script, when you print out $ab for the second time, you will treat ab as a variable and then print the value of ab, which is obviously empty; the second time you use ${a} b, you will print the value of an and then print a character b to STDOUT.

8. Parentheses ()

Parentheses can be used to define an array variable, as follows:

Array1= (1 2 3 4 5) / / define an array variable array2= (one two three four five) in shell

The value of the array element is also taken with the $symbol, as follows:

$echo $array1 $1$ echo ${array1 [2]} # take an element in the array and enclose the index in square brackets. As in most languages, the index of the array is $3$ echo ${array2 [0]} $one $echo ${array2 [*]} # outputs the entire array $one two three four five

The ${} expression is used here for variable substitution.

Note: using array variables in shell sometimes causes problems, and the portability of array variables is not good, so array variables are not used much in shell programming.

9. Parentheses (())

The double parenthesis command allows the use of advanced mathematical expressions during comparison:

((expression))

Where expression can be any mathematical assignment or expression. Compared with the test command, which can only use simple arithmetic operations in comparison, the double parenthesis command provides more mathematical symbols, which can perform a variety of logical operations and mathematical operations in double parentheses, and also supports more operators (such as +, -, etc.).

Double parentheses are commonly used to implement C-style iterations in for loops:

#! / bin/bash for ((I = 0; I)

< 10; i++)) do echo -n "$i " done echo "" exit 0 10、中括号 [ ] 单个的中括号的功能与 test 命令一样,都是用作条件测试。 #!/bin/bash read -p "please enter a number: " num if [ $num -gt 10 ]; then echo "num >

10 "else echo" num

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