Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I find the value of an environment variable?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

What is EOF command?

the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). CTRL-Z. key combination is used to stop a process. It can be used to put something in the background temporarily.

What is the use of EOF in shell scripting?

The EOF operator is used in many programming languages. This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

What is EOT in shell script?

EOT is an ASCII character that historically signalled the end of a message (and is a special character in UNIX terminals that means end of stream when it appears in user input only), but it CAN appear in files, so using it in C to signal the end of a file would be a terrible idea when reading binary files!


Remove the backslash before EOF:

#!/bin/bash

i=ok

# This prints "Bwah ok"
cat <<EOF
Bwah $i
EOF

# This prints "Bwah $i"
cat <<\EOF
Bwah $i
EOF

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

i=1
val1=beep
val2=bop

rightval="val$i"
cat <<EOF
This 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=0
vals=(beep bop)

cat <<EOF
This is a beep: ${vals[$i]}
EOF

Note however that the indexes start at 0.