What is the difference between `cat EOF` and `cat EOT` and when should I use it?
I’m used to use
cat > /path/to/file << EOF when I, in a bash script, printed more than one line into a file… I was checking old code of my company and I found the
cat EOT instruction instead of the
cat EOF I’m used to (please notice the T instead of the F at the end of it) and curiosity bit me.
I did a quick research and I only found this other question, but I think it was not related to what I wanted to know.
I did some tests with the following code:
cat > ./hello.txt << EOT
And I get the exact same output as when I use
EOF instead of
EOT. The output is, as expected:
root@test_VM:~# bash test.sh && cat hello.txt
So the questions are:
- What are the differences between the use of
- When should I use one over the other?
There is no difference, and no particular meaning to those two strings, or any others. It’s just an arbitrary terminator and you can use almost any string you like.
Of course, the data itself can’t contain that particular line, so if your data contains e.g. a shell script that has another here-doc, you’ll need to use different terminators in both. Using somewhat descriptive strings may be useful for any future readers of the script.
cat > test.sh <<END_OF_SCRIPT
test.sh which, when executed through the shell prints
There is a difference if you quote the terminator in the line that starts the here-doc, though, it’ll prevent expansions in the here-doc data. This prints
$i, not whatever the value of the variable is:
cat << 'EOF'
Let me answer your second question first.
That is, you should use whatever your project coding style guideline requires – or if there isn’t one, your own consistent preference.
The code editor I use generates "EOF" whenever I type the here-doc operators eventhough I personally prefer "!" which is actually not a special character to the shell. (It’s a reserved word that happen to be usable as a here-doc delimiter)
Back to the first question. EOT is actually an ASCII control character that, by convention on Unix systems, generates the End-Of-File indication to the program reading terminal input, when the terminal settings had not been otherwise altered.
So distinguishing EOF and EOT can in a way signify the purpose of the here-doc content, in addition to delimiting overlapping portions of different here-doc.
Finally. That other question you linked asked for explanation why EOF has a different value than EOT. And as explained, EOF is a condition, whereas EOT is a valid byte value and character, and EOF is generated by a EOT character under default terminal settings.