SoFunction
Updated on 2025-03-09

A classic explanation of shell single and double quotes for one Linux command every day

01 Classic Explanation

Single quotes: What you see is what you get

Double quotes: What you see is not what you get, it will parse the variable first and then output it

Backticks (``): Command replacement, usually used to pass the command output result into a variable

Backslash ( \ ) : escape characters/escape characters. If Linux wants to make escape characters work, it must use the -e option, and escape characters must use double quotes.

02 Example Demonstration

Please take a look at the following example:

[root@dev2~] echo "$HOME"

/root

[root@dev2~] echo '$HOME'

$HOME

From the above example, we can reflect the explanation that what you see in single quotes is what you get, and what you see in double quotes is not what you get. When you see in double quotes, the system will first calculate the value of $HOME and then echo it. The single quotes directly display the content in the single quotes.

my_name='wzx'
str="Hello, I know your are \"$my_name\"! \n"

Limitations of single quote strings:

Any character in single quotes will be output as it is, and variables in single quotes strings are invalid;

Single quotes cannot appear in single quote strings (not after using escape characters for single quotes).

Advantages of double quotes:

There can be variables in double quotes

Escape characters can appear in double quotes

Next, take a look at the example of backticks (``)

#!/bin/bash
#Demonstrate the backtick functionRESULT=`md5sum /home/wzx/`
#RESULT=$(md5sum /home/wzx/)
echo"MD5 value is: $RESULT"

Let’s take a look at the backslash: it is generally used as escape characters, or escape characters. If Linux wants to make escape characters work, it must use the -e option, and the escape characters must use double quotes.

echo-e "\n"

Another function of backslash is that when backslash is used for the last character of a line, the Shell uses the backslash at the end of the line as a continuation line. This structure is often used when inputting long commands in several lines.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.