BASH - how can i get the variable value inside the EOF tags?

拈花ヽ惹草 提交于 2019-12-02 22:12:15
Pikrass

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!