Zsh run command saved in variable?

In a shell script (in .zshrc) I am trying to execute a command that is stored as a string in another variable. Various sources on the Internet say that this is possible, but I do not get the behavior that I expect. Maybe this is ~ at the beginning of the command, or maybe using sudo , I'm not sure. Any ideas? Thanks

 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 } 

Output:

 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 
+8
scripting shell zsh
source share
2 answers

Use eval :

 eval ${install_cmd} 
+16
source share

As explained in & sect; 3.1 "Why $var , where var="foo bar" doesn’t do what I expect?" From the Z-Shell FAQ , you can use the shwordsplit shell option to tell zsh that you want to separate variables into spaces and treat them as a few words. This page also discusses alternatives that you might consider.

+9
source share

All Articles