0
  1. Hi all, I'm facing an issue that I cant read the environment variable from the text file.
  2. Here is the content of the text file:

Blockquote

#INCLUDE
$ward/ancd/qwe
.........
.........

And the bash script

while IFS= read -r line
do
      echo "$line" # It shows $ward/ancd/qwe instead of tchung/folder/main/ancd/qwe
done < "$input"

Blockquote

It should directly shows "tchung/folder/main/ancd/qwe" when echo, but it outputs $ward/ancd/qwe. The $ward is an environment variable and it able to shows the file path in bash when echo directly. But when comes to reading text file it cant really recognize the environment variable.

Blockquote

The current solution that i can think off is replace the matched $ENVAR in the $line with the value.

repo="\$ward" 
if echo "$line" | grep -q "$repo"
then
     # echo "match"
      line="${line/$repo/$ward}"
      #echo "Print the match line : $line"
fi

Is there any other more flexible way that can recognize the environment variables during reading file without replacing the substring one by one?

2 Answers2

0

Perhaps you need to evaluate the content of $line within an echo:

while IFS= read -r line
do
  echo $(eval "echo $line")
done
Carlos R.
  • 251
  • 3
  • 8
0

Use envsubst:

envsubst "$input"
user1934428
  • 19,864
  • 7
  • 42
  • 87