The difference and use of single quotes, double quotes, backquotes and escape characters in Shell programming in one article

foreword

In Shell programming, not only variables are used, but also symbols such $as , \, single quotes, double quotes, and backquotes are often used.

These symbols are sometimes confusing in use, so organize this article for easy learning, I hope it will be useful to you

1. Introduction and comparison of symbols

The following is a simple comparison of several symbols:

  • The dollar sign $is mainly used to quote variable values. For example, when defining variables MXS=moxiaosheng, you need to use them when quoting $MXS;
  • \Backslash is mainly used to escape specific characters and retain the original meaning, for example, echo “\$MXS”the result will be printed $MXS, but not printed moxiaosheng;
  • Single quotation marks ' ', also known as strong quotation marks, do not have the function of variable replacement, and all arbitrary characters are restored to the literal meaning, realizing the function of shielding Shell metacharacters;
  • Double quotes " ", also known as weak quotes, have the function of variable replacement and retain the function of $(使用变量前导符), \(转义符), ` (backquote) metacharacters;
  • Backtick ``, the backtick, is the key on the row above the Tab key on the keyboard, used for command substitution (equivalent to $(…)).

2. The difference between single quotes and double quotes

In the shell, single quotes (') and double quotes (") have the following differences when dealing with strings:

2.1. Variable expansion:

  • Variables within single-quoted strings will not be expanded and will be output as-is. For example, echo 'Hello $name' outputs "Hello $name".
  • Variables within double-quoted strings are expanded to their corresponding values. For example, echo "Hello $name", if the value of the variable name is "Alice", the output will be "Hello Alice".
#!/bin/bash

name="Alice"

# 单引号字符串中的变量不会展开
echo 'Hello $name'  # 输出结果为 "Hello $name"

# 双引号字符串中的变量会展开
echo "Hello $name"  # 输出结果为 "Hello Alice"

2.2 Command Substitution

  • Command substitutions within single-quoted strings will not be performed, and the commands will be output as-is. For example, echo 'Today is $(date)' outputs "Today is $(date)".
  • Command substitutions within double-quoted strings are performed and the output of the command is inserted into the string. For example, echo "Today is $(date)" will execute the command "date" and insert the output date into the string.

2.3 Processing of escape characters

  • Within single-quoted strings, escape characters (such as \n, \t) are treated as ordinary characters only and are not treated specially.
  • Inside double-quoted strings, escaped characters are interpreted and replaced with their meaning. For example, echo "Hello\tWorld" outputs "Hello World", where \t is interpreted as a tab character.

2.4 Nesting of quotation marks

  • Single-quoted strings can contain double quotes, and double-quoted strings can contain single quotes. For example, echo 'He said, “Hello”' and echo “It's a nice day” are both legal.

Just remember one thing: single quotes are usually output as-is.

3. Escape character

3.1 The concept of escape character

Escape characters are used in shell scripts to solve special character handling problems. When you need to insert a character with a special meaning in a string, you can use an escape character to cancel its special meaning and treat it as an ordinary character.

In shell scripts, a common escape character is a backslash (\), followed by the character that needs to be escaped.

3.2 Common escape characters

\n:换行符
\t:制表符
\":双引号
\':单引号
\\:反斜杠
\$:美元符号
```:反引号

3.3 Use cases

echo "This is a sentence with a newline.\nThis is on a new line."
echo "The price is \$10"  

This will output $the symbol

Use a script to complete a table with student names and grades:

#!/bin/bash

# 表头
echo -e "姓名\t成绩"

# 学生数据
echo -e "Alice\t90"
echo -e "Bob\t85"
echo -e "Charlie\t92"
echo -e "David\t87"

Output result:

姓名    成绩
Alice   90
Bob     85
Charlie 92
David   87

Implement a progress bar:

#!/bin/bash

echo "执行命令:yum install mariadb"
echo "开始安装..."

# 安装命令(取消输出)
yum install -y mariadb --quiet &

# 监控安装进度
while :
do
    sleep 1
    echo -n "#"
    if yum list installed mariadb >/dev/null 2>&1; then
        break
    fi
done

echo ""
echo "安装完成"

Use the yum install -y mariadb --quiet command to install the mariadb package and run it with & to start the background installation process. Then, use a while loop to check whether the mariadb package has been installed, and if the installation is complete, terminate the loop and output a message that the installation is complete.

Installation time still depends on several factors. But using the actual install command to monitor the progress would be closer to reality, and not kill the progress bar until the install is complete.

Summarize

The above is how to use various symbols, I hope it is useful to you, I am Mu Fengxiaoyue, I am waiting for you in csdn.

insert image description here

Guess you like

Origin blog.csdn.net/wisdom_futrue/article/details/131320384