Chapter 6 of linuxshell programming guide ------ quotation marks

Source: Internet
Author: User
The previous chapter describes the variables and replacement operations. The most common mistake in performing variable replacement in a script is the reference error. It is very important to reference in the command line. Here, only the reference basic rules are described. There are many examples to be referenced. The next two parts of this book will introduce the variables and replacement operations in the previous chapter. The most common mistake in performing variable replacement in the script is that

Reference error. It is very important to reference in the command line.

Here, only the reference basic rules are described. There are many examples to be referenced. The next two parts of this book will be one by one
. When executing the row operation in the script, s h e l will explain the script settings. We need to take a way to prevent s h e l from
Example: use a reference number, including various types of references or use a backslash.

The following lists various reference types.
"" Double quotation marks (") reverse quotation marks (")
''Single quotes \ backslash

Double quotation marks can be used to reference any character or string except the characters $, ', and. These special characters are dollar signs,
Backquotes and backslashes are of special significance for s h e l. If double quotation marks are used to assign a string to a variable and provide feedback
It is actually no different from the direct feedback variable.

[Root @ localhost huangcd] # string = "nihao huang cheng du"
[Root @ localhost huangcd] # echo "$ string"
Nihao huang cheng du
[Root @ localhost huangcd] # echo $ string
Nihao huang cheng du

Because s h e l assigns the string in the "" symbol to the variable m y d a t e, d a t e has no specific meaning, so the variable only saves the single
Word d a t e.

[Root @ localhost huangcd] # mydate = "date"
[Root @ localhost huangcd] # echo $ mydate
Date

Double quotation marks are often used to query strings containing spaces. The following uses g r e p to extract the name "Davey Wi r e ",
Because no double quotation marks are added, g r e p treats "D a v e y" as a string and "Wi r e" as a file name.

To solve this problem, double quotation marks can be added to the string. In this way, space is ignored for s h e l. when characters are used
Use double quotation marks, whether it is a single string or multiple words.

[Root @ localhost huangcd] # grep "Sabayon user"/etc/passwd
Sabayon: x: 86: 86: Sabayon user:/home/sabayon:/sbin/nologin

Variables can be caused by double quotation marks in a feedback text line. In the following example, s h e l returns the text line,
When the symbol $ is encountered, it is known that this is a variable, and then replace the variable $ B o y with the variable value B O Y.

[Root @ localhost huangcd] # boy = "BOY"
[Root @ localhost huangcd] # echo "The $ boy did well"
The BOY did well
[Root @ localhost huangcd] # echo "the" $ boy "did well"
The BOY did well

The single quotation marks are similar to double quotation marks. The difference is that s h e l ignores any referenced values. In other words, if the special
Meaning. all characters in quotation marks, including quotation marks, are used as a string. The result of the previous example is as follows:

[Root @ localhost huangcd] # girl = 'Girl'
[Root @ localhost huangcd] # echo "the '$ gir' did well"
The 'gir' did well
[Root @ localhost huangcd] # echo 'the '$ gir' did well'
The GIRL did well

Backticks are used to set the output of system commands to variables. S h e l uses the content in the quotation marks as a system command, and
Execute its content. This method can replace the output with a variable. Backticks can be used with backticks.

[Root @ localhost huangcd] # echo 'Hello'
Bash: hello: command not found
[Root @ localhost huangcd] # echo 'date'
Saturday, December 07, 2013 10:51:55 CST

[Root @ localhost huangcd] # date + % A "the" % e "of" % B "" % Y
Saturday the 7 of December 20 13

Print the number of users on the current system:

[Root @ localhost huangcd] # echo "'Who | wc-l '"
2

If the next character has a special meaning, the backslash prevents s h e l from misunderstanding its meaning, that is, shielding its special meaning. The following words:
It has special significance: & * + ^ $ '"| ?.

The preceding statement can also be used for the $ command. s h e l interprets it as the current process I D. Use a backslash to block this statement.
Print $.

[Root @ localhost huangcd] # echo \*
*
[Root @ localhost huangcd] # echo $
4936
[Root @ localhost huangcd] # echo \ $
$

Some problems may occur during reference and often errors occur. I follow two rules when using references:
1) the feedback string is enclosed in double quotation marks, but do not reference the feedback itself.
2) if the reference results are not satisfactory, try another method. after all, there are only three reference methods, and you can fully try them.
Try.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.