The difference between single quotes, double quotes, anti-quotes, and backslashes in the shell

Source: Internet
Author: User
Tags echo command



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: Shell scans the command line, sets the value of x to an asterisk *; the shell scans the command line again, touches an asterisk *, Replace 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 三 6月 15 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 " \ 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.



The difference between single quotes, double quotes, anti-quotes, and backslashes in the shell


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.