Mícéal Gallagher in Java 2 minutes

Bash - Parse a Java property file

So you need to parse a Java property file; lets say your property file resembles the one below:

propertyTwo=valueTwo
propertyThree=valueThree

You can easily read the value of each property like so:

. ~/Downloads/file.properties
echo "The value of propertyOne is $propertyOne"
echo "The value of propertyTwo is $propertyTwo"
echo "The value of propertyThree is $propertyThree"

Here be dragons!

Why is this method dangerous? Put simply, the file.properties is being (sourced) executed as if it was a script. This means if any malicious code is contained within the file, it will be run as the invoking user’s privilege level. This could be devastating if it is SU.

Property name contains “.”

This method of sourcing the properties file will not work if your property names contain periods like so:

property.two=valueTwo
property.three=valueThree

If you need an alternative method for parsing a property file other than the one stated here, head over to Shrubbery (great name) where Joshua Davis has a fantastic write up about various ways to accomplish this task, including an alternative version of sourcing the properties file.