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

1 Answers1

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.

Janis
  • 14,222