0

I have an app which runs and reads a text configuration file. This points to several locations of configurations / outputs etc.

Is it possible to use environmental variables inside the text configuration file, rather than hardcoded paths?

LogFilePath=$LOG_FILE_PATH vs LogFilePath=/home/user/logs

When running, the application fails as it cannot expand the Environment Variable. It will be sourced inside the shell before the application is run.

Thanks!

donalmg
  • 627
  • 5
  • 15
  • 22
  • Only if the environment variable `LOG_FILE_PATH` is declared before that line in the config file. There could be something like delayed expansion, too. But I'm not aware of it and it would depend on your exact shell, which you haven't stated. – 0xC0000022L Apr 19 '11 at 12:36

1 Answers1

1

Recently I used this in a (bash) script:

#!/bin/bash
# ...
source config.file
# ...

Where config.file had lines like this:

export ORIG_PATH="${PATH:-.}:/bla/bla"
export SOMESETTING="${SOMEVAR:-"somedefault"},somedata"
...

So the ${parameter:-word} thing worked well for me: use default values, if parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. (From man bash.)

HTH

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110