I have been trying to figure a way to copy a file, (a template), that has internal variables. Using the values as defined for those variables in another script.
So a file called x -
#! /bin/bash
D=aa.$X.bb
And file y
#! /bin/bash
X=6
while read line
do
eval echo "$line"
done < x
Output -
brad@TX5XN:~/wip$ y
D=aa.6.bb
brad@TX5XN:~/wip$
As you can see I lose the line with the #! from file x
If I redirect to a file -
brad@TX5XN:~/wip$ cat y
#! /bin/bash
X=6
while read line
do
eval echo "$line" > z
done < x
I lose all of the lines except the blank line from the end of the file -
eval takes the result of its parameters and starts expanding/executing it anew as if given from stdin. The # in #! /bin/bash makes this a comment line, so eval does not execute anything which results in the empty line that you see in your output.
Regarding your redirection: it does exactly what you told it to do: reset z to zero and overwrite it. Using >> z will append your output to z.