在shell腳本中(在.zshrc中)我試圖執行一個命令,該命令作爲字符串存儲在另一個變量中。網絡上的各種消息稱這是可能的,但我沒有得到我期望的行爲。也許這是在命令開始時的~
,或者可能是sudo
的使用,我不確定。有任何想法嗎?由於zsh運行存儲在變量中的命令?
function update_install()
{
# builds up a command as a string...
local install_cmd="$(make_install_command [email protected])"
# At this point the command as a string looks like: "sudo ~some_server/bin/do_install arg1 arg2"
print "----------------------------------------------------------------------------"
print "Will update install"
print "With command: ${install_cmd}"
print "----------------------------------------------------------------------------"
echo "trying backticks"
`${install_cmd}`
echo "Trying \$()"
$(${install_cmd})
echo "Trying \$="
$=install_cmd
}
輸出:
Will update install
With command: sudo ~some_server/bin/do_install arg1 arg2
trying backticks
update_install:9: no such file or directory: sudo ~some_server/bin/do_install arg1 arg2
Trying $()
update_install:11: no such file or directory: sudo ~some_server/bin/do_install arg1 arg2
Trying $=
sudo ~some_server/bin/do_install arg1 arg2: command not found
你可以使用'zsh -c'$ {install_cmd}'' – Alex