Linux: Shell Script to Prompt for a Password Using Dialog Window

Linux: Shell Script to Prompt for a Password Using Dialog Window

Below is an example of a shell script to prompt for a password using the dialog window in Linux/Unix systems.

Prompt for a Password Using Dialog Window in Linux/Unix

The following shell script will create a temporary file to store the user entered password and then will read the password using the cat command. The window is displaying for prompting for the password using the dialog command in Linux.

#!/bin/bash

# create a file to store the password
output="psw.txt"
# remove the password file, if already exists.

trap "rm -f psw.txt" 2 15
dialog --title "Password" \
--insecure \
--clear \
--passwordbox "Please enter password" 10 30 2> $output
reply=$?
case $reply in
0) echo "You have entered Password : $(cat $output)";;
1) echo "You have pressed Cancel";;
255) cat $data && [ -s $data ] || echo "Escape key is pressed.";;
esac

# getting the value into a variable
var=$(cat $output)

echo $var

# remove the password file
rm psw.txt

Output

The window output would be as shown in the featured image of this article. Below is the output which is being generated by the echo command:

You have entered Password : abcxyz
abcxyz

Note

If you have not installed the dialog in your Linux system, then use the following command to install it:

sudo apt-get install dialog

See also: