Question

I want my bash script to write a string to standard input stream. I want it to pass my password to git push command. I've tried the three following options and neither of them worked: git was asking me for the password.

#!/bin/bash

# 0
git push origin master <<< 'password'

# 1
echo 'password' | git push origin master

# 2
git push origin master <<EOF
password
EOF
Was it helpful?

Solution

All the three methods work pass the string password␤ to the command.

The problem is that the command does not read password from standard input. It reads it from controlling terminal, i.e. opens the special file /dev/tty and reads from there.

In case of ssh transport, it is actually the ssh that asks the password and it does it the above way (it thus follows that this discussion applies to any other use of ssh or tool based on ssh as well). In case of http transport git does it in the same way.

The reason is twofold:

  1. So that you can redirect the input and/or output of the actual command and still give password from keyboard and see the prompt on the terminal and
  2. to prevent the thing you are trying to do, which is strongly discouraged for security reasons.

See Git push: username, password, how to avoid? for how to use git remote commands without giving password each time. If you insist on http, the options in Is there a way to skip password typing when using https:// on GitHub? or Git http - securely remember credentials are more secure. See also gitcredentials(7).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top