I'm trying to start a program in the background on a remote server via ssh, but this program must be run from the directory it is contained within.

I tried doing the following

ssh -q user@server 'cd directory && command > /dev/null 2>&1 &'

But this results in the error

Ambiguous output redirect.

I am redirecting the output to /dev/null because otherwise the ssh call would not finish.

I have tried wrapping the command in {} and (), as well as with and without quotes but get the same error.

How can I work around this issue?

  • 2
    You've tagged this bash - are you sure user's remote shell is not a different shell - perhaps csh? – steeldriver Jul 19 '19 at 23:23
  • @steeldriver I'm running the ssh command from bash, but I believe the remote command will be run in csh. – flau Jul 19 '19 at 23:26
  • 1
    @steeldriver actually tcsh, not csh ;-) To the OP: any commands given to ssh will be run with the login shell of the user on the remote machine, whatever that is. Just try ssh -q user@server 'echo $version' – mosvy Jul 19 '19 at 23:29
  • 2
    In csh and tcsh you could redirect both stdout and stderr with command >& /dev/null (which, though deprecated also works in bash, but not in another bourne shells). – mosvy Jul 19 '19 at 23:34
  • 1
    @mosvy Thanks, that works - feel free to post it as an answer :) – flau Jul 19 '19 at 23:37

As mentioned in comments, the remote user's login shell is csh or tcsh. These shells have very different syntax to the various POSIX shells, and in particular, the syntax for redirections is different.

To redirect both standard output and standard error to /dev/null with these shells, use >& /dev/null:

ssh -q user@server 'cd directory && command >& /dev/null &'

Alternatively, explicitly invoke a POSIX shell to run the command:

ssh -q user@server 'sh -c "cd directory && command >/dev/null 2>&1" &'
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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