1
如何執行包含重定向字符的bash行(命令)? 例如,以下行不能執行。執行包含重定向字符的bash行(命令)
home>CMD="ls -l > out"
home>$CMD
ls: cannot access >: No such file or directory
ls: cannot access out: No such file or directory
感謝您的幫助提前。
如何執行包含重定向字符的bash行(命令)? 例如,以下行不能執行。執行包含重定向字符的bash行(命令)
home>CMD="ls -l > out"
home>$CMD
ls: cannot access >: No such file or directory
ls: cannot access out: No such file or directory
感謝您的幫助提前。
eval $CMD
會做你想做的。
您可以使用eval,但不推薦使用。如果將命令存儲在變量中的目的是多次執行,最好使用一個函數。
實施例:
dols() {
ls -l > out
}
dols
參見:I'm trying to put a command in a variable, but the complex cases always fail!
'eval'是一個錯誤磁鐵,並且應儘可能避免。如果你必須使用它,至少要雙引號(即'eval「$ CMD」')來避免[真正的離奇行爲](http://stackoverflow.com/questions/9308606/usr-bin-find-無法建立它的參數動態/ 9322047#9322047),可以來自命令運行通過額外的解析水平。 –