Ubuntu – Is it possible to pass passwords on a shell script


Long time i am searching for this, i would like to know is it possible to pass passwords in a shell script? Many of the answers returned with no. Recently i read an article stating that how to pass passwords in a shell script. I have tried that, but it doesn't seem to work. This is the link. Can anyone checkitout and revert back? Also pls say me is there a way to pass passwords in a shell script? If no pls say me how linux gets the input for the password?

Best Answer

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'