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…

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;

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

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:

$ ./

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

Follow us on social media