1

I'm trying to make a script where I use a command that ask for the user to input some data in the prompt.

Let's say that I want to include git push in a script (I know it isn't best practice, but everyone is going to understand what I'm asking this way.)

So when you use git push in the console, it asks for user and password. How can I pass them when git is called from a shell script, and both are stored as shell variables?

It should look something like

#!/bin/bash

git push
$user
$password
1
  • 1
    Reading usernames and passwords happens to often be a special case as these, in interactive situations, are seldom read from standard input but directly from the terminal device. Is your question specifically about how to automate git push, how to automate some other command that uses SSH, or is it about something that does not have to do with passing authentication credentials?
    – Kusalananda
    Nov 12, 2020 at 12:32

2 Answers 2

2

The easiest way seems to pipe it to the external command:

printf "%s\n%s\n" "$user" "$password" | command_expecting_username_and_passwd

This will output the content of $user and $password, each followed by a newline, and redirect the output to the stdin of the external process which should expect username and password in that order to be entered by the user if it were called interactively.

If you want to perform more sophisticated operations, you may want to look into scripts.

Note that, as stated by Kusalananda, this only works if the program is reading these from their stdin and not directly from the terminal device (which however seems to be usual when asking for authentication information), and will not work when actually applying it to git (which you presented as one usage example).

3
  • It didn't work for me. It still stops the execution at Username for 'https://github.com': Nov 12, 2020 at 11:48
  • Found this https://superuser.com/questions/1078435/piping-credentails-to-git-push. You can't pipe things to git push, but it works for other commands. Thanks! Nov 12, 2020 at 12:01
  • I suspect that the linked answer is misdiagnosing the issue -- git is probably opening /dev/tty directly rather than using its stdin in order to read the user and password, just like ssh does. In fact, I cannot think of many programs where your answer would work at all -- maybe some old versions of su (NOT the one from Debian).
    – user414777
    Nov 12, 2020 at 19:34
0

The kind of tool to you are looking for is expect.

https://linux.die.net/man/1/expect

It is a tool to automate interaction with a TTY.

Keep in mind that it is not secure, and to push to github without a password, you should use ssh

git remote add origin git@github.com:name/repo

though the remote "origin" is probably already taken in your repository.

https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh

Once you have set a ssh public and private key and you added them to your ssh agent, you won't need to type a password to push or pull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.