I'm trying to do this as an April Fool's prank: make a linux machine display a message in the shell every few seconds.My thought is to achieve this by starting an infinite loop that runs as a background job (in .bashrc).For example, this does what I want:while true ; do echo Evil Message; sleep 10; doneIn order to run it in background I tried:cmd=while true ; do echo Evil Message; sleep 10;$cmd &but this fails with the error:while: command not foundWhy do I get the error? Is there a way to make this script work?
while: command not found
bash;shell;quoting
while is not a command, it's a shell keyword. Keywords are recognised before variable expansion happens, so after the expansion, it's too late.You have several options:Don't use a variable at all.while true ; do echo Evil Message; sleep 10; done &Use eval to run the shell over the expanded value of the variableeval $cmd &Invoke a shell to run the loopbash -c $cmd &Use a function (that's what is typically used to store code):cmd() { while true ; do echo Evil Message; sleep 10; done; }cmd &