Differences between single quotes, double quotes, backquotes, and backslash in linux shell

Source: Internet
Author: User
Tags echo command

1. Single quotes ('')

# Grep Susan phonebook
Susan Goldberg 403-212-4921
Susan Topple 212-234-2343
If we want to search for Susan Goldberg, we cannot directly use the grep Susan Goldberg phonebook command. grep regards Goldberg and phonebook as files to be searched.
# Grep 'Susan gold' phonebook
Susan Goldberg 403-212-4921
When the shell encounters the first single quotation mark, it ignores all the special characters that follow it until the right quotation mark.

2. Double quotation marks ("")

Double quotation marks are similar to single quotes. The difference is that they are not so strict. Single quotes tell shell to ignore all special characters, while double quotes only need to ignore the majority. Specifically, three special characters enclosed in double quotes are not ignored: $ ,\,', double quotation marks explain the special meaning of strings, while single quotation marks directly use strings. if you use double quotation marks to assign a string to a variable and feed it back, it is actually no different from the direct feedback variable. Double quotation marks are often used to query strings containing spaces.
# X = *
# Echo $ x
Hello. sh menus. sh misc. sh phonebook tshift. sh
# Echo '$ x'
$ X
# Echo "$ x"
*
In this example, we can see the difference between no quotation marks, single quotation marks, and double quotation marks. In the last case, double quotation marks Tell shell to replace the variable name in the quotation marks, so shell replaces $ x with * because the double quotation marks do not replace the file name, therefore, * is passed to echo as the value to be displayed. For the first case, it should be further explained that shell does not replace the file name when assigning values to variables (this can also be seen in the third case). The exact sequence of each step is as follows: shell scans the command line and sets the value of x to asterisk *;
Shell scans the command line again, encounters the asterisk *, replaces it with the file list in the current directory;
Shell starts to execute the echo command and passes the file list as a parameter to echo.
The order of value assignment is very important: shell replaces the variable first, then the file name, and finally processes this line as a parameter.

3. Reverse quotation marks ('')

Command replacement means that shell can insert the standard output of a command to any location in a command line. There are two methods in shell to replace the command: Use reverse quotation marks or $ (...) for shell commands (...) structure, where $ (...) the format is supported by the POSIX standard and also facilitates nesting.
# Echo The date and time is 'date'
The date and time is three June 15 06:10:35 CST 2005
# Echo Your current working directory is $ (pwd)
Your current working directory is/home/howard/script

4. backslash-escaped (\)

The backslash is generally used as an escape character or escape character. In linux, if echo is used to enable the escape character, the-e option must be used and double quotation marks must be used for escape characters.

Echo-e "\ n"

Another function of the backslash is that when the backslash is used for the last character of a line, shell uses the backslash at the end of the line as the continuation line, this structure is often used when several lines of long commands are input.

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.