Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my script I do something like this:

command="some/path/ arg1 arg2; some/path/ arg1 arg2;"
ssh_command="ssh root@$ip '$command'"

echo $ssh_command
exec $ssh_command

The echo gives output like this:

ssh root@ 'some/path/ arg1 arg2; some/path/ arg1 arg2;'

After the "exec" thing I get the output:

bash: some/path/ arg1 arg2; some/path/ arg1 arg2;: No such file or directory

However, when copy the command from echo output and run it directly from the terminal it works like charm. Any ideas why?

share|improve this question

exec replaces the shell with a program, invoking it with the arguments supplied. The shell sees 2 tokens: "exec", and "some/path/ arg1 arg2; some/path/ arg1 arg2;". The second argument is interpreted as a path to a program to execute, including all the spaces and semicolons. Only a shell knows to split arguments on whitespace and to separate commands at a semicolon. Therefore, you should replace the exec with a call to a shell, like sh -c "$ssh_command".

share|improve this answer
Or, do what you probably really meant to: eval "$ssh_command". The quotes might not be necessary. If you want this to end the script, use eval exec $ssh_command. – Scott Feb 6 '13 at 18:26

Nohup may be useful to you.

Try integrating something like this from the link below.

ssh -n -f user@host "sh -c 'cd /whereever; nohup ./whatever > /dev/null 2>&1 &'"

share|improve this answer
nohup is a useful tool, but the part of your command that actually solves the problem is the "sh -c", not nohup. – bonsaiviking Feb 6 '13 at 15:27
Thanks for the clarification. – Enigma Feb 6 '13 at 15:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.