I am working on some shell and I encountered the following line: cat /ssh/sshkey | tr -d '\r' | ssh-add -

This code get a key and add it to an agent. This work on its current environment, but when I try it on another VM, I get the following: Error loading key "(stdin)": invalid format

ssh-add expect a prompt with a passphrase, which I believe is what this error is about, but this is in a script and should work without one.

What am I missing ?

  • 1
    Is there a tangible difference in that file between the two systems? Do these systems have different ssh versions installed?
    – Jeff Schaller
    Jun 16, 2022 at 17:20
  • What operating system is running on your two (virtual) machines?
    – fra-san
    Jun 16, 2022 at 17:46
  • To debug, replace | ssh-add - with | od -bc - to see what-s happening.
    – waltinator
    Jun 16, 2022 at 22:12
  • it is running on Red Hat Enterprise Linux Server release 7 (Maipo) on both sides. It is to note that on one side it is in a ksh script (launched from a jenkinsfile), while the other is in a gitlab-ci.yml file, and the image doesn't have ksh. od -bc - showed the key in memory
    – Saurazim
    Jun 17, 2022 at 8:07

1 Answer 1


It's quite simple; ssh-add uses /dev/tty and not stdin because it needs to disable the echo of the typed password so you can't simply redirect to it.

In fact the manual page states:

If any file requires a passphrase, ssh-add asks for the passphrase from the user. The passphrase is read from the user's tty.

So you need to run ssh-add attached to a pty, or you can invent a clunky hack with SSH_ASKPASS (I don't recommend it). Either expect(1) (the canonical behemoth) or something simpler like pdip; zsh has a module called zpty maybe there's something similar in bash too.

  • Thank you for all these informations, i will note this as the answer then.
    – Saurazim
    Jun 20, 2022 at 16:16

You must log in to answer this question.

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