a peek into my MIND

January 18, 2010

Reading a properties file in linux shell script.

Filed under: General — Bharat Kondeti @ 10:12 pm

It’s pretty easy to read a key=value style properties file using some sed and grep magic in a shell script

Example properties file:




Following script takes in a profile name and read’s the profile information accordingly

workspace=`sed '/^\#/d' environment.properties | grep "$1_workspace"  | tail -n 1 | sed 's/^.*=//'`
profile=`sed '/^\#/d' environment.properties | grep "$1_profile"  | tail -n 1 | sed 's/^.*=//'`
serverLocation=`sed '/^\#/d' environment.properties | grep "$1_serverLocation"  | tail -n 1 | sed 's/^.*=//'`
serverType=`sed '/^\#/d' environment.properties | grep "$1_serverType"  | tail -n 1 | sed 's/^.*=//'`
remoteMachine=`sed '/^\#/d' environment.properties | grep "$1_remoteMachine"  | tail -n 1 | sed 's/^.*=//'`

sed ‘/^\#/d’ — This will skip any line’s that start with ‘#’. ‘/d’ is similar to ‘-v’ option in grep.
grep “$1_workspace” — will grep for the respective profile line
tail -n 1 — if for some reason there are multiple grep matches pick only first one.
sed ‘s/^.*=//’ — Substitution magic is happening over here. I am replacing the string part till ‘=’ with empty string.

So if I pass in DAS03-8090 as profile name to the script, a value ‘/opt/home/dasadmin/workspace/branches/Production-2010-01-14’ will be set to the variable ‘workspace’



  1. Awesome! Just what i needed. But SED is pretty complicated for newbies man! Thanks a lot!

    Comment by Shravan — July 6, 2010 @ 11:55 am

  2. Awesome! Beautiful solution!

    Comment by Anonymous — October 20, 2010 @ 8:28 pm

  3. What happens when the value contains a special character?
    the line contains a $ character.


    and The value should be print as “development-reso$ur#@ces”

    Comment by bikash — February 6, 2011 @ 9:03 pm

  4. Hi,
    I have also faced some issues:
    – several values are chosen by grep from keys
    – value contains ‘=’ signs

    To solve this I’ve changes the proposed approach to:

    about_link=`sed ‘/^\#/d’ environment.properties | grep “about.link.$1[ =]” | tail -n 1 | sed ‘s/[^=]*=//’`

    Nevertheless, thanks man, this gave me a good starting point.

    Comment by yuradizhak — July 19, 2012 @ 5:36 am

  5. Nice post… really helpful

    Comment by anonymous — October 22, 2013 @ 2:24 pm

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: