0
git fetch someremote  # the remote is on github.com, and using SSH

the above command succeeds if I issue it from a terminal, but if I put it in a bash script, it will fail, with:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I hope this gives someone some clue as to help figure out why it fails in the bash script but succeeds at a terminal! Anyone have any idea?

Same thing seems to happen if I use either SSH or HTTPS

9
  • 4
    Do you use an SSH agent? That would explain why you can connect to github over ssh after having entered your password, and cannot automate it. Commented Nov 1, 2016 at 21:59
  • 1
    Also, what user are you while calling the script? Your normal user or do you sudo the script? Commented Nov 1, 2016 at 22:12
  • 1
    @Vrakfall either sudo or no sudo, same thing Commented Nov 1, 2016 at 22:57
  • 1
    It's better not to sudo, in this case. :P Also, are you using a specific ssh agent, as @EricDuminil asked and/or a specific ssh key, aka other than ~/.ssh/id_rsa? Commented Nov 1, 2016 at 22:59
  • No SSH agent to my knowledge, I set up SSH keys the standard way with Github using .ssh/id_rsa etc Commented Nov 2, 2016 at 0:10

1 Answer 1

2

Probably a problem with your environment variables. Why not try diffing them?

$ echo "printenv | sort > /tmp/script.env" > /tmp/envtest.sh
$ bash /tmp/envtest.sh
$ printenv | sort > /tmp/term.env
$ diff /tmp/term.env /tmp/script.env

In my terminal I get:

76c76
< SHLVL=1
---
> SHLVL=2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.