How can I get the variable value inside the EOF tags? How can I get the variable value inside the EOF tags? linux linux

How can I get the variable value inside the EOF tags?


Remove the backslash before EOF:

#!/bin/bashi=ok# This prints "Bwah ok"cat <<EOFBwah $iEOF# This prints "Bwah $i"cat <<\EOFBwah $iEOF

To get your last line display rightsubnet="10.109.0.20/32" (for i=1), you need something like this:

i=1val1=beepval2=boprightval="val$i"cat <<EOFThis is a beep: ${!rightval}EOF

That is, you compute the name of the variable you want, put that in another variable, and use the ${!var} syntax.

But for that kind of thing you should rather use an array:

i=0vals=(beep bop)cat <<EOFThis is a beep: ${vals[$i]}EOF

Note however that the indexes start at 0.