Mosty Mosty - 22 days ago 8
Linux Question

bash: How to replace all variable names with their values after `cat` a file?

A.txt
contains the following:
$x + $y = $z


I want to
cat A.txt
which will echo
$x + $y = $z
, but I want the variables to be replaced with their values, Any way?

Let's look at another simple test:
B.txt
contains
$x
, we can use
echo `echo \$x`
but neither
echo $(echo \$x)
nor
echo `cat B.txt`


Why
cat
dosn't work? and how to do it?

Answer

improve @Ipor answer

x=2
y=1
z=3
while read -r line
do
  eval echo $line
done < A.txt