Mike Slinn

Reading Java Properties Files from Bash

Published 2004-11-03.
Time to read: 1 minutes.

This page is part of the posts collection, categorized under Java, Scripting.

Ever notice that Java properties files are fairly similar to bash scripts? Here is a short bash script that takes a valid Java properties file called language.properties, massages it into a format that is compatible with bash syntax, writes it to a temporary file and sources it.

Shell
#!/bin/bash
TEMPFILE=$(mktemp)
cat language.properties | \
  sed -re 's/"/"/'g | \
  sed -re 's/=(.*)/="\1"/g' > $TEMPFILE
source $TEMPFILE
rm $TEMPFILE

After running this script, all of the properties in the Java properties file become available as environment variables.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.