I am trying to create a script that adds my SSH private key to ssh-agent.
#!/bin/bash
ssh-agent /bin/sh
ssh-add passphrase_encrypted_key
If I run these commands from the command line, I am prompted for the passphrase after the ssh-add command. If I run them as a script, I'm never prompted for a passphrase.
I know this has something to do with subprocessing and/or a sub-shell being created, but I just can't find the information that points me in the right direction.
I have tried commands, "set -m", exec, placing the command in left right parenthesis and maybe a few others that I can't think of right now.
For any response that I receive, a good reference on understanding the fundamentals on how bash scripting works with sub-processes, etc. would really help.
FYI, the ssh-agent is not started by default on the machine that I am using and I am not the admin.
CodePudding user response:
After much more research, I was able to figure out that I needed to add the following line to my script:
eval "$(ssh-agent -s)"
I don't understand why I need the $, because I thought that was only for referencing variables defined within the shell script, so I have more research to do.
My final basic script looks like:
#!/bin/bash
eval "$(ssh-agent -s)"
ssh-add passphrase_encrypted_key
After the script executes the line which adds the key to the agent, I'm prompted for the passphrase as expected.
Thank you to everyone that has responded.