Skip to content

Latest commit

 

History

History
122 lines (94 loc) · 4.72 KB

File metadata and controls

122 lines (94 loc) · 4.72 KB

4.3. Bash Variables Are Untyped

第四章第三節:Bash 變數是沒有特定類型的

Unlike many other programming languages, Bash does not segregate its variables by "type."

不同於其他許多程式語言,Bash 並不區分變數的資料型別。

Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables.

Bash 變數其實實質上為字串類型,但 Bash 可依照所處腳本內容允許對其做算術運算與比較。

The determining factor is whether the value of a variable contains only digits.

決定因素為變數值是否只包含數字。

Example 4-4. Integer or string?

範例 4-4. 是整數?還是字串?

#!/bin/bash
# int-or-string.sh

a=2334                     # Integer.
                           # 整數。
let "a += 1"
echo "a = $a "             # a = 2335
echo                       # Integer, still.
                           # 仍為整數。

b=${a/23/BB}               # Substitute "BB" for "23".
                           # This transforms $b into a string.
                           # 以 "BB" 代替 "23",將使 $b 轉換成字串。
echo "b = $b"              # b = BB35
declare -i b               # Declaring it an integer doesn't help.
                           # 將它重新宣告成整數並沒有幫助。
echo "b = $b"              # b = BB35

let "b += 1"               # BB35 + 1
echo "b = $b"              # b = 1
echo                       # Bash sets the "integer value" of a string to 0.
                           # 算術運算時,字串會被 Bash 當成整數零 (0)。

c=BB34
echo "c = $c"              # c = BB34

d=${c/BB/23}               # Substitute "23" for "BB".
                           # This makes $d an integer.
                           # 以 "23" 代替 "BB",將使 $d 成為整數。
echo "d = $d"              # d = 2334
let "d += 1"               # 2334 + 1
echo "d = $d"              # d = 2335
echo

# What about null variables?
# 那麼空字串變數 (null variables) 會是如何呢?
e=''                       # ... Or e="" ... Or e=
                           # ... 或寫成 e="" 或 e= 都可以。
echo "e = $e"              # e =
let "e += 1"               # Arithmetic operations allowed on a null variable?
                           # 允許對空字串變數做算術運算?
echo "e = $e"              # e = 1
echo                       # Null variable transformed into an integer.
                           # 空字串變數確實已轉換成整數。

# What about undeclared variables?
# 那麼未宣告的變數會是如何呢?
echo "f = $f"              # f =
let "f += 1"               # Arithmetic operations allowed?
                           # 允許算術運算?
echo "f = $f"              # f = 1
echo                       # Undeclared variable transformed into an integer.
                           # 未宣告的變數已轉換成整數。
#
# However ...
# 不過 ...
let "f /= $undecl_var"     # Divide by zero?
                           # 會是除零嗎?
#   let: f /= : syntax error: operand expected (error token is "/= ")
# Syntax error! Variable $undecl_var is not set to zero here!
# 語法錯誤!變數 $undecl_var 在這裡並未被設成整數零 (0)!
#
# But still ...
# 但 ...
let "f /= 0"
#   let: f /= 0: division by 0 (error token is "0")
# Expected behavior.
# 仍舊為預期行為。

#  Bash (usually) sets the "integer value" of null to zero
#+ when performing an arithmetic operation.
#  But, don't try this at home, folks!
#  It's undocumented and probably non-portable behavior.
#  (通常) Bash 在做算術運算時,會把空字串當成整數零 (0)。
#  但是請各位鄉民別跟著在家試!
#  這點並無明文規定,且在不同平台可能會有不同行為,
#  這將使得腳本變得不可攜 (non-portable)。

# Conclusion: Variables in Bash are untyped,
#+ with all attendant consequences.
# 結論:所有上述結果都導向 Bash 變數是不分型別的。

exit $?

Untyped variables are both a blessing and a curse.

無型別變數有好有壞。

They permit more flexibility in scripting and make it easier to grind out lines of code (and give you enough rope to hang yourself!).

它讓撰寫腳本更有彈性,並使撰寫大量的程式碼更不費力!

However, they likewise permit subtle errors to creep in and encourage sloppy programming habits.

然而,它也容易在不知不覺中產生細微的錯誤,並且變相鼓勵草率的程式設計習慣。

To lighten the burden of keeping track of variable types in a script, Bash does permit declaring variables.

為了減輕持續追縱腳本中變數型別變化的負擔,Bash 允許明確地宣告特定型別的變數。