Following up on the top answer to this question, I thought I could:1)  Define a command: cmd='for x in $(my_command | grep keyword | cut -d  -f1); do command2 arguments $x; done'2) Run it on a loop as followswhile true; do $cmd; sleep 1; doneHowever, the above doesn't work, and I get the followingzsh: command not found for x in $(......zsh: command not found for x in $(......zsh: command not found for x in $(.........Any thoughts why?Clarification:If I run for x in $(my_command | grep keyword | cut -d  -f1); do command2 arguments $x; done' it works perfectly.Addendum:I have noticed that if I use eval, it works, i.e.:while true; do eval $cmd; sleep 1; doneruns the command cmd every second
Why doesn't the following work?: while true; do $my_command; sleep 1; done
shell script;zsh
By putting quotes around $cmd, you're asking the shell to run a command named exactly that. There is not command named for x in $(...; in fact on my system, there isn't even a command named for - it's a shell keyword. To run the contents of $cmd, you'll have to use eval.