我在bash中寫了一個小腳本,但它只在將字符串作爲變量存儲時才起作用,並且我想知道原因。以下是摘要:bash之間的原始字符串和字符串之間的差異
當我使用字符串本身,慶典將其視爲一個單一實體
for word in "this is a sentence"; do
echo $word
done
# => this is a sentence
如果我救了完全相同的字符串轉換成變量,bash中遍歷的話
sentence="this is a sentence"
for word in $sentence; do
echo $word
done
# => this
# is
# a
# sentence
- 爲什麼這些被區別對待?
- 有沒有一種簡單的方法來遍歷字符串中的單詞,而無需首先將字符串保存爲變量?
1.請了解報價。 – 2014-11-06 21:34:46
2.然後嘗試:'在這是一句話;做回聲「$ word」; done'。 – 2014-11-06 21:35:41
'bash -x' ...... – 2014-11-06 21:37:30