How to Read a Properties File in Linux Shell Scripting


In this lesson, we are going to see how to read a properties file properly in Linux Shell Scripting. Let’s give it a go…

Linux shell scripting is a powerful tool for automating tasks and streamlining workflows in a Linux environment. One common task in Linux shell scripting is reading configuration data from a properties file. A properties file is a simple file format that contains key-value pairs, which can be used to store configuration data for a script or application.

We will walk through a step-by-step example of reading data from a properties file and using it in a shell script. By the end of this tutorial, you will have a clear understanding of how to read properties files in Linux and how to use them in your own scripts and applications.

We will have our properties file and the properties reader shell script file in this example. Save these two files in the same working directory with the following contents;

Method 1: Using “grep”

1. Our properties file will hold values in a key-value pair:

2. The file has the actual property reader functions:

#read the properties file and returns the value based on the key
function getPropVal {
    value= grep "${1}" ./$FILE_NAME|cut -d'=' -f2
	if [[ -z "$value" ]]; then
	   echo "Key not found"
	   exit 1
	echo $value

#Get the value with key
function testPropertyVal {
NAME=($(getPropVal ''))
echo $NAME

#Call the testPropertyVal function

Run the script:

$ ./

Method 2: Using the “source” or ".” command

1. Define the properties file: Define the properties file and its location in the shell script. For example, if the properties file is named “” and is located in the same directory as the shell script.


You can use the following command to define it:


2. Read the properties file: Use the “source” or “.” command to read the properties file and load the properties into variables in the shell script. For example, if the properties file contains the property database.username with the value admin, you can use the following command to load it into a variable in the shell script:

source $PROPS_FILE

This command will load the value of database.username from the properties file into the variable USERNAME in the shell script.

3. Use the properties in the shell script: You can now use the properties in the shell script as you would any other variable. For example, you can use the USERNAME variable in a SQL query to connect to the database:

mysql -u $USERNAME -p

This command will use the value of the USERNAME variable to connect to the MySQL database.

Complete example:

source $PROPS_FILE

mysql -u $USERNAME -p

Also, see file manipulation and how to Find file in Linux . See complete examples in our GitHub repository.

Follow us on social media