Git fetch命令在命令行中成功,但在bash脚本中失败

Git fetch命令在命令行中成功,但在bash脚本中失败,git,bash,github,Git,Bash,Github,如果我从终端发出上述命令,则该命令将成功,但如果我将其放入bash脚本中,则该命令将失败,如下所示: git fetch someremote # the remote is on github.com, and using SSH 我希望这能给某些人一些线索,帮助他们找出为什么它在bash脚本中失败,但在终端上成功了!有人知道吗 如果我使用SSH或HTTPS,同样的情况似乎也会发生,这可能是您的环境变量有问题。为什么不试试区分它们呢 Permission denied (publickey

如果我从终端发出上述命令,则该命令将成功,但如果我将其放入bash脚本中,则该命令将失败,如下所示:

git fetch someremote  # the remote is on github.com, and using SSH
我希望这能给某些人一些线索,帮助他们找出为什么它在bash脚本中失败,但在终端上成功了!有人知道吗


如果我使用SSH或HTTPS,同样的情况似乎也会发生,这可能是您的环境变量有问题。为什么不试试区分它们呢

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

Please make sure you have the correct access rights
and the repository exists.
在我的终端,我得到:

$ 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
76c76
SHLVL=2

您的环境变量可能有问题。为什么不试试区分它们呢

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

Please make sure you have the correct access rights
and the repository exists.
在我的终端,我得到:

$ 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
76c76
SHLVL=2

您是否使用SSH代理?这就解释了为什么在输入密码后可以通过ssh连接到github,而不能自动执行密码。另外,调用脚本时您是什么用户?你的普通用户还是你在使用脚本?@Vrakfall要么使用sudo,要么不使用sudo,同样的道理在这种情况下,最好不要使用sudo:P另外,您是否按照@EricDuminil的要求使用特定的ssh代理和/或特定的ssh密钥,即除
~/.ssh/id\u rsa
?据我所知,没有ssh代理,我使用Github的标准方式设置ssh密钥。ssh/id\u rsa etcDo您使用ssh代理吗?这就解释了为什么在输入密码后可以通过ssh连接到github,而不能自动执行密码。另外,调用脚本时您是什么用户?你的普通用户还是你在使用脚本?@Vrakfall要么使用sudo,要么不使用sudo,同样的道理在这种情况下,最好不要使用sudo:P另外,您是否按照@EricDuminil的要求使用特定的ssh代理和/或特定的ssh密钥,即
~/.ssh/id\u rsa
?据我所知,没有ssh代理,我使用.ssh/id\u rsa等使用Github的标准方式设置ssh密钥