Is it possible to pass passwords on a shell script?
By "entering passwords", you likely mean entering data without being visible for the user.
(suggested by geirha) When using bash, you can use the -s
option to prevent typed characters from being displayed:
read -p "Password please: " -s pass
Alternatively, change the behavior of the terminal to hide typed characters with stty -echo
(disable echo
). After reading the password with the shell built-in read
into a variable (in the below example, $pass
), turn it back on with stty echo
. Because the new line from Enter is hidden to, you've to print a newline to get future output on a new line.
stty -echo
read -p "Password please: " pass
stty echo
printf '\n'
read
and printf
are shell built-ins. stty
is provided by the coreutils
package which is installed by default. That means that this snippet is very portable.
Note: the -p
option is not standard, but from bash
. If you need to display a prompt in other shells, use:
printf "Password please: "
stty -echo
read pass
stty echo
printf '\n'
References:
- Manual page of
stty
- Manual page of
bash
To which program do you want to pass a password?
The script on the link works for me. Note that is not a shell script but an expect script (needs the package expect
to be installed`). Using expect is a common way to automate text based interactive programs.
Non-interactive ssh
logins are often done using key-based authentication with an empty passphrase.
Some other programs (like sudo
) have options to read a password from stdin.
Providing a password as a command line option is often a security problem as on most systems any user can see any other users processes including there command line arguments using simple tools like ps
.