1

In the shell script, I want to grep a value from the log file and set as the value of one environment. In the log file, there is one line like this:

SOME_PATH=/some/specific/path

In my shell script, I grep the SOME_PATH keyword, and I got the above line and split it with =, but I'm now able to set the environment variable:

line=`grep "SOME_PATH" /path/to/the/log.log`
path=${line##*=} 
export SOME_PATH="$path"  #I cannot change the environment variable with this line.

And if I just have the script like below in my bash file, the environment variable changes if I source the bash file.

export SOME_PATH=/some/specific/path
2
  • 2
    Are you expecting the variable's value to change in the parent shell? if so you will need to source the script rather than run it - see Using '.' to execute files in bash Commented Apr 14, 2015 at 10:10
  • Hi, I used source to run my file, but with grepped value, the env variable won't be set. Commented Apr 14, 2015 at 10:27

1 Answer 1

2

In principle your solution should work, but it is not robust. If, for example, you have in the greped file a comment (or something else) in the matched line, say, like:

SOME_PATH=/some/path # this is some path

then the comment will also be part of the name that you expand in path. To check that inspect what's actually in the path, e.g. by printf "'%s'\n" "$path".

Note that setting an environment variable this way will only affect the shell instance where you set it and any subprocesses, but not the surrounding environment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .