This bash script is supposed to fetch a token from elsewhere via ssh, then run a curl command where it reads data from a file on stdin.

#!/usr/bin/env bash
TOKEN=$(ssh somewhere cat sometoken.txt)
curl https://example.com \
  -H "Authorization: Bearer $TOKEN" \
  -H "someparam=@-" \

And the script is called like this:

cat somefile | myscript

When I hardcode TOKEN=abcd123 instead of using $(ssh ...), somefile is uploaded as expected by curl.

However when I use $(ssh ...) my file is not uploaded.

I assume that my script's stdin is being clobbered by $(ssh ...) but I can't see how to work around that.

Any help would be much appreciated.

  • Are you sure the ssh has completed in the script? Is it waiting for a password? Also, I think you want cat instead of echo :).
    – JamesL
    Feb 3, 2020 at 16:40
  • I'm sure ssh has completed because I can add an echo $TOKEN before the curl and it writes it out on stdout. You're right about cat v echo - though neither is the command I actually run. Feb 3, 2020 at 16:45

1 Answer 1


In short, use ssh -n ...

The ssh tool reads all data from standard input. It does this to be able to pass it on to any command that you are executing on the remote side (you could use ssh remote 'cat >out' <in to copy the contents of a file to a remote system if you so wished).

With its -n option, you tell it to not do that. It would be more or less equivalent to redirecting /dev/null into its standard input by doing ssh ... </dev/null.

  • Thank you very much, that works! Feb 3, 2020 at 17:11
  • And thank you for the clear explanation. Feb 3, 2020 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .