"cat" command: how to automatically escape everything that might be a violation?

13,677

Solution 1

Compare the two here documents in the following example:

(yeti@darkstar:6)~/wrk/tmp$ cat ./xyzzy 
#!/bin/bash
cat << EOF
Version 1 - Today is $(date)
EOF
cat << 'EOF'
Version 2 - Today is $(date)
EOF
(yeti@darkstar:6)~/wrk/tmp$ ./xyzzy 
Version 1 - Today is Sa 21. Jun 08:51:38 CEST 2014
Version 2 - Today is $(date)

Solution 2

Use quotes around the "EOF" string:

cat > /test <<'EOF'
stuff $(pwd)
EOF

outputs

stuff $(pwd)

literally.

See the bash manual on heredocs. Any quotes in the terminator string prevent any expansions and substitutions in the body.

Share:
13,677

Related videos on Youtube

user72685
Author by

user72685

Updated on September 18, 2022

Comments

  • user72685
    user72685 almost 2 years

    If you copy the contents of

    httpd.conf
    

    and then paste it into a cat command.. such as this one..

    #!/bin/bash
    cat > /test << EOF
    pasted here..
    EOF
    

    you run into this error:

    -bash: command substitution: line 1: unexpected EOF while looking for matching `''
    -bash: command substitution: line 4: syntax error: unexpected end of file
    

    perhaps the solution is to escape the dollar signs and maybe even quotes and so on..

    but given that this is such a large file.. can dollar signs be automatically be escaped ?

    is my only option to escape them via another program and then give it to the cat command ?