Home » Linux/Unix » Shell Script Example to Prompt for a Password in Linux

Shell Script Example to Prompt for a Password in Linux

In Linux, use read command with -s option to read, hide and store password into a variable. Below is a shell script example, in which it will prompt for a password for Oracle database user SYSTEM, and after logging into the database, it will display the version information of Oracle.

Shell Script Example to Prompt for a Password

prompt_password.sh

#!/bin/bash
echo "Enter password for user System: "
read -s system_pw
sqlplus -S system/$system_pw@//YourIP/orcl << EOF
set pagesize 0 linesize 80 feedback off

SELECT BANNER
  FROM v$version;
  
Exit;
EOF

exit

Test

$ ./prompt_password.sh

Output

Enter password for user System: 
_
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE	11.2.0.1.0	Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

See also: