How to write exact string inside block instead of executing them? While using << EOF in Bash?

421 views Asked by At

I have a file configure.sh with following (it creates a test.sh file with configuration, so that i can use that test.sh finally as main configuration task). But it does not work

cat > /var/tmp/test.sh << EOF
regex='value=(.*)'
for i in $(cat /var/tmp/test.ini);
do
    if [[ $i =~ $regex ]];
    then
        echo ${BASH_REMATCH[1]} 
        #or
        curl -v ${BASH_REMATCH[1]}
    fi
done
EOF

When the configure.sh is executed it makes the test.sh file completely wrong such as reged='value(.*)'

for i in 
original line1
original line1
original line1
original line1
do
  if [[ =~ ]];
  then 
  fi

done
EOF

The EOF block is not writing exactly how i set above. How do you write such string inside EOF?

2

There are 2 answers

1
glglgl On BEST ANSWER

From man bash, section Here Documents:

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.

So write << \EOF instead of << EOF.

0
Michael Hoffman On

Change << EOF to << 'EOF' and leave the EOF at the end unchanged. Quoting the EOF in this way will stop Bash from executing expansions on the here-document.