在一个shell脚本(在.zshrc中)中,我试图执行一个以字符串形式存储在另一个变量中的命令。网络上的各种消息来源都说这是可能的,但是我没有得到我期望的行为。我不确定这是命令开头的~,还是使用sudo。有任何想法吗?谢谢

function update_install()
{
    # builds up a command as a string...
    local install_cmd="$(make_install_command $@)"
    # 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

最佳答案

使用eval

eval ${install_cmd}

关于shell - zsh运行存储在变量中的命令?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13665172/

10-11 12:51
查看更多