Linux Bash – load declared variable from textfile

Issue

My situation :

Textfile : test.txt

$test

Variable test declared in the terminal

test=hello

Now when I cat the file, output is :

$test

My question how can I cat the file with the declared output from the variable?

Solution

cat itself does not evaluate any variables. To do it, the command line must be evaluated by bash twice. This is done by the eval command. Thus the result should be

eval echo $(cat test.tex)

Answered By – guthondr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published