Search code examples
bashunixpasswords

How to provide password to a command that prompts for one in bash?


I'm writing a UNIX shell function that is going to execute a command that will prompt the user for a password. I want to hard-code the password into the script and provide it to the command. I've tried piping the password into the command like this:

function() {
    echo "password" | command
}

This may not work for some commands as the command may flush the input buffer before prompting for the password.

I've also tried redirecting standard input to a file containing the password like this, but that doesn't work either:

function() {
    echo "password" > pass.tmp
    command < pass.tmp
    rm pass.tmp
}

I know that some commands allow for the password to be provided as an argument, but I'd rather go through standard input.

I'm looking for a quick and dirty way of piping a password into a command in bash.


Solution

  • Take a look at autoexpect (decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.