1

I have the env file:

FOO=123

I need to read it and set its variables. I try to do it in my script:

#!/usr/bin/bash
cat env | grep '^[^#]' | while IFS== read key value; do
    echo "echo: $key=$value"
    export "$key=$value"
done
echo "FOO -> $FOO"

The output:

echo: FOO=123
FOO ->

Why I get the FOO -> output instead of FOO -> 123?

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • 1
    Because you're piping input into the `while` loop, which runs it in a subshell. See e.g. https://www.oreilly.com/library/view/bash-cookbook/0596526784/ch19s08.html. – larsks Aug 06 '21 at 19:40
  • Well, I would start by reading the link I included in my earlier comment. There are lots of questions here on stack overflow that cover the same topic. – larsks Aug 06 '21 at 19:42
  • Also the BASH FAQ has an [entry on this subject](http://mywiki.wooledge.org/BashFAQ/024). – larsks Aug 06 '21 at 19:43

0 Answers0