Article
home/blog/Loading

How to read from a properites file using Bash

Reading from a properties file is a set some config outside of the script for ease of editing.

This allows you to have some more advanced input options compared to the default args.

Reading from a properties file

name=John
last=Wiseman

To access the values of a properies file in sh you can use this function getProperty. The function takes one argument which is the intended property you want the value from.

function getProperty {
    cat file.properties | grep $1 | cut -d'=' -f2 | tr -d '"'
}

You can then call the function like the following

echo $(getProperty 'name')

John

How it works

  1. Call function with an argument of property you want
  2. The function then gets the contents of the properties file cat file.properties
  3. The file contents are filted based on $1 the property you want
  4. The line matching the property is cut so the result will return what is after the =
  5. Removes any " which exist