~2011

Cat output without interpretation

I usually use cat in a bash script to create files. However sometimes the text for output through cat is interpreted and this causes script errors or wrong files. Especially with ASCII art output. The fix for this is to quote the 'EOF' string.

For example creating a ASCII art output through a script. If I do the following:

#!/bin/bash
cat << EOF
                                  ********************_
                                    /        **_    ******\   
                                   /       /'**_`\ /\  **_  
                                  / ****  /\_\ /\ \\ \ \__/ \  
                                 / /\_ ,`\\/_/// /**\ \**_``  
                                /\ \/_/  /_  // /_\ \\/\ \L\  
                                \ \  /\****\/\******/ \ \**__/ \
                                 \ \ \/****/\/****_/   \/___/   \
                                  \ \****************************\
                                   \/****************************/
EOF

it will result in

bash:  / : No such file or directory
bash: /: is a directory
bash: /: is a directory
                                  ********************_
                                    /        **_    ******\   
                                   /       /'**_\/_/// /**\ \___\
                                /\ \/_/  /_  // /_\ \/\ \L\ \
                                \ \  /\****\/\******/ \ \**__/          

The fix for this is to quote the end string with single quotes. If you use no quotes or double quote the text will be interpreted by cat. With single quotes it's taken literal.

#!/bin/bash
cat << 'EOF'
                                  ********************_
                                    /        **_    ******\   
                                   /       /'**_`\ /\  **_  
                                  / ****  /\_\ /\ \\ \ \__/ \  
                                 / /\_ ,`\\/_/// /**\ \**_``  
                                /\ \/_/  /_  // /_\ \\/\ \L\  
                                \ \  /\****\/\******/ \ \**__/ \
                                 \ \ \/****/\/****_/   \/___/   \
                                  \ \****************************\
                                   \/****************************/
EOF