1

I have created the utility that need to be run in all the system without any error by creating the required environment. When ever I run the following command it will take the path value from a config file and that should assign that value into BASE_DIR variable

export BASE_DIR=`cat <config file path> | grep BASE_DIR | head -1 | cut -d = -f 2`

Following is the content of config file:

BASE_DIR=$HOME/flshrc

when I run the first code it is taking value as $HOME/flshrc instead of the actual home path, for example /home/user1/flshrc

How to not consider the grepped value as string and should be considered as the actual value and substitute the $HOME value?

Thank you.

  • 1
    can't you just run source <config file path> ? – pLumo Oct 15 '18 at 12:23
  • Yeah, I see you're cat-ing the contents of the file but the shell is not processing that line. So you could do $(cat yourfile.conf) to get it to process. Are you after basename $(echo $HOME)? – pbhj Oct 15 '18 at 13:00
2

You can use the envsubst utility. For example, given the following configfile

foo=bar
BASE_DIR=$HOME/flshrc
USER=$USER

then

$ envsubst < configfile | awk -F= '$1 == "BASE_DIR" {print $2}'
/home/steeldriver/flshrc
1

You should consider a different method to provide configuration to your script:

config:

foo=bar
export BASE_DIR=$HOME/flshrc

main:

source config
echo "$BASE_DIR"

source filename [arguments] - Read and execute commands from filename in the current shell environment


You probably should add the path to the main script to the source command, otherwise the config file is searched for in the current directory:

main:

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source "$SCRIPT_DIR/config"
echo "$BASE_DIR"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.