How to suppress variable substitution in bash heredocs

Is it possible to create a heredoc that does not undergo variable expansion?

eg.

   cat <<-EOF  > somefile.sh
   Do not print current value of $1 instead evaluate it later.
   EOF

Update . I know about shielding \. My actual heredoc has many variables in it - and it is a mistake, prone and tedious, to avoid all of them.

+4
source share
2 answers

Enter a separator:

cat <<-"EOF"  > somefile.sh
Do not print current value of $1 instead evaluate it later.
EOF

It leads to:

$ cat somefile.sh 
Do not print current value of $1 instead evaluate it later.

Documentation

The format of the docs here is:

          <<[-]word
                  here-document
          delimiter

, , . < > , , - . , , , \ , \ \, $ `.

< < -, , , . - . [ .]

+3

$

$ VAR=XXX
$ cat << END
> dk
> \$VAR
> END
dk
$VAR
0

Source: https://habr.com/ru/post/1599798/


All Articles