Differences in the use of single quotation marks (') and double quotation marks (") in shell scripts [reprint]

Source: Internet
Author: User
Tags echo command

The shell can recognize 4 different types of cited symbols: single quote character ' double quote character ' backslash character \ anti-quote character '

1. Single quotation mark (')
# grep Susan Phonebook
Susan Goldberg 403-212-4921
Susan Topple 212-234-2343
If we're looking for Susan Goldberg and can't use the grep Susan Goldberg Phonebook command directly, GREP will treat Goldberg and phonebook as files that need to be searched
# grep ' Susan Gold ' phonebook
Susan Goldberg 403-212-4921
When the shell encounters the first single quotation mark, it ignores any special characters that fall behind until the closing quotation mark

2. Double quotation marks ("")
Double quotation marks are similar to single quotes, except that they are not so strict. Single quotes tell the shell to ignore all special characters, whereas double quotes only require ignoring most, specifically, three special characters enclosed in double quotes are not ignored: $,\, ', that is, double quotes will explain the special meaning of the string, Instead, single quotes use strings directly. If you use double quotation marks to assign a string to a variable and feed it back, there is actually no difference from the direct feedback variable. If you want to query a string that contains spaces, you often use double quotation marks.
# x=*
# echo $x
hello.sh menus.sh misc.sh Phonebook tshift.sh
# echo ' $x '
$x
# echo "$x"
*
This example shows the difference between quotation marks, single quotes, and double quotes. In the last case, the double quotation marks tell the shell to replace the variable name in quotation marks, so the shell replaces $x with * Because the file name is not replaced in double quotes, so the * is passed to echo as the value to be displayed. For the first case, it is further explained that the shell does not replace the file name when assigning a value to the variable (as can be seen from the third case), and the exact sequence of steps occurs as follows: The shell scans the command line and sets the value of x to an asterisk *;
The shell scans the command line again, touches the asterisk *, and replaces it with the list of files in the current directory;
The shell initiates the echo command and passes the file list as parameters to echo.
The precedence of this assignment is important: The shell replaces the variable first, then replaces the file name, and finally handles the line as a parameter

3. Anti-quote (')
Command substitution refers to the ability of the shell to interpolate the standard output of a command anywhere on a command line. There are two ways to make command substitutions in the shell: put the shell command in reverse quotation marks or $ (...). Structure enclosed, where, $ (...) Formats are supported by POSIX standards and are also beneficial for nesting.
# echo the date and time is ' date '
The date and time is 36 months 06:10:35 CST 2005
# echo Your Current working directory is $ (PWD)
Your Current working directory Is/home/howard/script.

4. Backslash backslash-escaped (\)
Backslashes are generally used as escape characters, or escape characters, and Linux if Echo wants the escape character to work, use the-e option, and the escape character uses double quotation marks
Echo-e "\ n"
Another effect of the backslash is that when the backslash is used for the last character of a line, the shell takes the backslash as a continuation line, which is often used when a few lines of input long command.
BTW
The difference between $ () and '
The function of the anti-quote and $ () is to replace the command, to execute the string in the back quotation mark or $ () as a command, and we use it to assign a variable to the execution result of the system commandwhen programming with she LL.
However, there is no reference to the anti-quote in the inverted quotation mark, and the reverse quotation mark can be quoted in $ ()
[[email protected] b]# cat AA
13
45
Mimi Bibi
[Email protected] b]# data= ' cat ' ls '
No response, I'm CTRL + C.
[[email protected] b]# data=$ (cat ' ls ')
[Email protected] b]# echo $data
Mimi Bibi

The author "Happy First"

Differences in the use of single quotation marks (') and double quotation marks (") in shell scripts [reprint]

Related Article

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.