如何在單引號字符串中使用變量?我只是想知道如何在單引號內回顯變量(我使用單引號,因為字符串中有引號)。 echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}任何幫助將不勝感激
3 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
變量以雙引號字符串擴展,但不在單引號字符串中擴展:
$ name=World $ echo "Hello $name" Hello World $ echo 'Hello $name' Hello $name
如果您只需切換引號,請執行此操作。
如果您更喜歡使用單引號來避免額外的轉義,則可以在同一參數中混合和匹配引號:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.' single quoted. Double quoted. Single quoted again. $ echo '"$name" has the value '"$name" "$name" has the value World
適用于您的案例:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
使用printf:
printf 'test text "here_is_some_test_text_%s" "output"\n' "$counter" >> ${FILE}

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
使用heredoc:
cat << EOF >> ${FILE}test text "here_is_some_test_text_$counter" "output"EOF
添加回答
舉報
0/150
提交
取消