7

I am using Github for Windows on Windows 7. I have a bash script to add the ssh-key to my ssh-agent. I have setup a ssh remote repo.

add_key.sh

#!/bin/bash    
cd ../ssh/
eval $(ssh-agent)
ssh-add id.rsa
cd ../htdocs/

Execute command-

./add_key.sh

It returns

Agent pid 5548
Identity added: id.rsa (id.rsa)

When I git push origin master, it fails. But when I manually cd in the ssh directory, and run the same ssh-related commands and cd back to my directory htdocs and git push to origin master, it works.

Why is this happening?

9
  • Where are you running the git push from? Some other shell session? Does that shell-session have the ssh-agent environment options set correctly? Commented Apr 22, 2015 at 18:54
  • Same shell session. I have not setup any ssh-agent env options. I am running the git push from inside the repo folder (htdocs). Commented Apr 22, 2015 at 18:57
  • Are you using ./source to run that "script" or are you using ./script.sh? Because the latter means your git push cannot be in the same session as that script runs in its own shell session. Commented Apr 22, 2015 at 18:59
  • 1
    The extension doesn't matter. How you run it does. Running a script as /path/to/script.sh starts a new shell. Using . /path/to/script.sh uses the current shell. Commented Apr 22, 2015 at 19:07
  • 2
    No, that's /path/to/script.sh. Your path just happens to be ./. You want . add_key or . ./add_key. Note the space. Commented Apr 22, 2015 at 19:16

1 Answer 1

9

Your problem is that your script is running in its own shell session because you are running ./add_key.sh.

This means that the variables set by eval $(ssh-agent) are not living beyond that shell session so the parent session doesn't have them and cannot use the agent (also you might be spawning a new agent each time you run the script).

The fix for this is to run that "script" in the current session by dot-sourcing the script instead of running it as an external script.

That is you want to use . add_key.sh.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.