Reputation: 163
I've got a file with variable exports like this:
$ cat file.txt
export A=foo
export B=\"B-\$A\" # I want export another var, which depends on a previously exported var
Using eval
to execute those statements does not produce the expected behavior:
$ eval $(cat file.txt)
$ echo ${B:-unset}
unset
When not using export
, it works as expected:
$ eval $(echo "A=foo B=\"B-\$A\"")
$ echo ${B:-unset}
B-foo
Why the difference, and is there any way to achieve the same result without removing the export
from the variable A
(that part of the file comes from an external source outside my control, but I can change the variable B)?
Upvotes: 1
Views: 2865
Reputation: 249642
First, change file.txt
to this (and consider naming it file.sh
instead):
export A=foo
export B="B-$A"
Then, instead of eval $(cat file.txt)
, do this:
source file.txt
That does the same thing as if you had pasted the content file.txt
into your shell.
Upvotes: 2