Reading a Password from a File in Linux

This tutorial is to show you how to read a password from a file in Linux using the shell script.

For the below example, I am creating a single file to store the password for each user. Suppose you want to store the password for three users then you need to create three files. For example, the filename will be the username, and it will save the password for that user only.

To store the password files, I have created a directory at /home/vin/test/passwords. Here is how I set up my passwords directory for everyone but the user who owns it.

Everyone can access it only, but the owner can create and delete.

$ cd /home/vin/test
$ ls -ld passwords


drwxrwxr-x 2 vin vin 4096 Mar 26 19:07 passwords

Example - Create a Password File in Linux

To create a password file in Linux, I am using the echo command by redirecting its output to a file to write the password for a specific user quickly. In the below example, It will create a password file for the user SYSTEM and storing the password VINISH.

$ cd passwords
$ pwd
$ echo 'vinish' >
$ ls
$ cat

The password file is ready now. We can use it in our shell script as showing in the following example.

Example - Reading a Password from a File in Linux

The below shell script will read the password from the file using the cat command and will store the password into the variable system_pw.


system_pw=`cat /home/vin/test/passwords/`
sqlplus -S system/$system_pw@//YourIP/orcl << EOF
set pagesize 0 linesize 80 feedback off

SELECT    'Database '
       || instance_name
       || ' has been running since '
       || TO_CHAR (startup_time, 'HH24:MI MM/DD/YYYY')
  FROM v\$instance;



$ ./


Database orcl has been running since 13:22 03/14/2019

See also:

Leave a Comment