Submitting a password after an input prompt using bash/shell script

Daniel Ellis Research
2 min readOct 28, 2020

Often on machines, you are faced with a script where you have to supply arguments or credentials, but can only do so after an input prompt. This is due to poor coding practice (from the developers) and requires ‘ad-hoc’ solution- one which we try to come up with below.

Expecting — Text after Prompts

The solution comes in the form of expect. This is available on most Linux systems. We start by spawning our command which generates the prompt, and if the prompt contains a desired keyword or phrase (e.g. ‘Password’) we reply with our return string and a newline ( \n ) character.

The following script runs <command_to_run> and then enters the first argument ( $1 ) provided to the script.

— Don't forget to make this executable with chmod a+x <scriptname.sh>

#!/bin/bash/usr/bin/expect <<EOD
spawn <command_to_run>
expect "Password"
send "$1\n"
expect eof
EOD

We can now run our code with ./scriptname.sh myNotSoSecurePassword .

Spawning a file you then need to Source

If the script you are interested however produces environmental variables you want to use and is generally sourced using …

--

--

Daniel Ellis Research

Research Software Engineer specialising in High-Performance Computing and Data Visualisation. — PhD in Atmospheric Chemistry and Masters in Theoretical Physics.