Skip to content

Latest commit

 

History

History
180 lines (126 loc) · 6.07 KB

ch5-1.md

File metadata and controls

180 lines (126 loc) · 6.07 KB

5.1. Quoting Variables

第五章第一節:引用變數

When referencing a variable, it is generally advisable to enclose its name in double quotes.

當參考到一個變數時,通常建議使用雙引號將變數名稱包起來。

This prevents reinterpretation of all special characters within the quoted string -- except $, \`` (backquote), and \` (escape). [30]

這可以防止所有的特殊字元被額外解讀 ─ 除了 $ (錢字號), (反引號), 與 \ (反斜線;掉脫字元)。 [30]`

Keeping $ as a special character within double quotes permits referencing a quoted variable ("$variable"), that is, replacing the variable with its value (see Example 4-1, above).

錢字號 $ 在雙引號裡面保持為特殊字元,允許用來參考一個被雙引號包住的變數 ($variable),也就是將變數替換成其變數值。 (詳情請參閱:第四章 "變數與參數" 的第一節 "變數替換"。)

Use double quotes to prevent word splitting. [31]

使用雙引號來防止字組分割問題 (word splitting)。 [31]

An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators.

被雙引號包住的引數將被視為單一字組,就算裡面含有空白分隔字元。

List="one two three"

                   # 從空白字元處分割變數內容
for a in $List     # Splits the variable in parts at whitespace.
do
  echo "$a"
done
# one
# two
# three

echo "---"

                   # 保留單一變數中所有空白字元
for a in "$List"   # Preserves whitespace in a single variable.
do #     ^     ^
  echo "$a"
done
# one two three

A more elaborate example:

一個更複雜的範例:

variable1="a variable containing five words"
COMMAND This is $variable1    # Executes COMMAND with 7 arguments:
                              # 執行指令 COMMAND 附帶 7 個引數:
# "This" "is" "a" "variable" "containing" "five" "words"

COMMAND "This is $variable1"  # Executes COMMAND with 1 argument:
                              # 執行指令 COMMAND 附帶 1 個引數:
# "This is a variable containing five words"


variable2=""    # Empty.
                # 空字串。

COMMAND $variable2 $variable2 $variable2
                # Executes COMMAND with no arguments.
                # 執行指令 COMMAND 不帶任何引數。
OMMAND "$variable2" "$variable2" "$variable2"
                # Executes COMMAND with 3 empty arguments.
                # 執行指令 COMMAND 附帶 3 個空白引數。
COMMAND "$variable2 $variable2 $variable2"
                # Executes COMMAND with 1 argument (2 spaces).
                # 執行指令 COMMAND 附帶 1 個引數 (內含 2 個空格字元(space))。

# Thanks, Stéphane Chazelas.
# 感謝 Stéphane Chazelas 所提供。

Enclosing the arguments to an echo statement in double quotes is necessary only when word splitting or preservation of whitespace is an issue.

僅當字組分割或空白字元呈現是個問題的時候,才需要對 echo 描述句裡的引數用雙引號包住。

Example 5-1. Echoing Weird Variables

範例 5-1. Echoing 奇怪的變數

#!/bin/bash
# weirdvars.sh: Echoing weird variables.
#               Echoing 奇怪的變數。

echo

var="'(]\\{}\$\""
echo $var        # '(]\{}$"
echo "$var"      # '(]\{}$"     Doesn't make a difference.
                 #              沒差。

echo

IFS='\'
echo $var        # '(] {}$"     \ converted to space. Why?
                 #              反斜線 (\) 被轉成空格字元。為什麼?
echo "$var"      # '(]\{}$"

# Examples above supplied by Stephane Chazelas.
# 上述範例由 Stephane Chazelas 所提供。

echo

var2="\\\\\""
echo $var2       #    "
echo "$var2"     #  \\"
echo
# But ... var2="\\\\"" is illegal. Why?
# 但是... var2="\\\\"" 是不合法。為什麼?
var3='\\\\'
echo "$var3"     # \\\\
# Strong quoting works, though.
# 不過單引號括住是可以動的。


# ************************************************************ #
# As the first example above shows, nesting quotes is permitted.
# 如同之前第一個範例,允許使用巢狀引號。

echo "$(echo '"')"           # "
#    ^           ^


# At times this comes in useful.
# 有時這派得上用場。

var1="Two bits"
echo "\$var1 = "$var1""   # $var1 = Two bits
#    ^                ^

# Or, as Chris Hiestand points out ...
# 亦或,如同 Chris Hiestand 所指出的 ...

if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]]
#     ^     ^         ^ ^     ^     ^         ^ ^
then
  ...
fi
# ************************************************************ #

Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special meaning of $ is turned off.

單引號 (' ') 與雙引號 (" ") 的運作方式類似,但因為關閉了錢字號 $ 的特殊意義,所以不允許引用變數。

Within single quotes, every special character except ' gets interpreted literally.

在單引號裡面,除了單引號本身之外的所有特殊字元皆以字面上的原意解讀。

Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial quoting").

單引號 ("完整引用") 可認定是一個比雙引號 ("部份引用") 更嚴謹的引用方法。

Since even the escape character (\\) gets a literal interpretation within single quotes, trying to enclose a single quote within single quotes will not yield the expected result.

即使因為連跳脫字元 (\) 都被單引號以字面原意解讀,但試圖用單引號包住單引號並不會有同樣的預期結果。

echo "Why can't I write 's between single quotes"

echo

# The roundabout method.
# 迂迴的方式。
echo 'Why can'\''t I write '"'"'s between single quotes'
#    |-------|  |----------|   |-----------------------|
# Three single-quoted strings, with escaped and quoted single quotes between.
# 三段由單引號包住的字串,之間還有跳脫的與雙引號包住的單引號。

# This example courtesy of Stéphane Chazelas.
# 感謝 Stéphane Chazelas 提供範例。