[英]Bash - Executing a command in a script adding single quotes to a variable containing spaces
[英]How to retain quotes when executing a command in bash
我有一個bash腳本,其中包含類似於以下內容:
cmd="grep 'a b'"
echo $cmd
$cmd
我遇到的問題是$ cmd由於某種原因刪除了'a b'周圍的引號,並以grep ab
執行命令,導致它出錯:
% ./test.sh
grep 'a b'
grep: b': No such file or directory
我嘗試了各種引號和轉義的組合,但結果總是一樣的。
存儲命令的最佳位置是函數。 經驗法則:變量用於數據; 函數用於命令。
cmd() {
grep 'a b'
}
...
cmd
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.