4

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:

password=hello
cat > ./hello.txt << EOT
authentication {
    auth_type PASS
    auth_pass $password
  }
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

authentication {
    auth_type PASS
    auth_pass hello
  }

So the questions are:

  1. What are the differences between the use of EOT and EOF?
  2. When should I use one over the other?

2 Answers 2

23

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.

E.g.

cat > test.sh <<END_OF_SCRIPT
cat <<EOF
hello
EOF
END_OF_SCRIPT

produces test.sh which, when executed through the shell prints hello.

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'
$i
EOF

See also:

12
  • 3
    @k.Cyborg The label could be used for documentation, to a degree. For example, your code in the question could use AUTH_CONF_END or a similar label.
    – Kusalananda
    Jun 8 at 19:24
  • 1
    Beware there are some limitations as to what characters can be used as the delimiter. If it's otherwise a character special to the shell such as ;, (, ', #, then it has to be quoted, and then than means expansions are no longer performed inside. Even when quoted, using newline in the delimiter works in few shells (only dash and ksh93 in the few shells I tried). Jun 8 at 20:08
  • 5
    I've used things like :||:<<'# some comment' for instance to comment out some code up to # some comment for instance. Jun 8 at 20:08
  • 1
    @StéphaneChazelas, why the :|| ?
    – ilkkachu
    Jun 8 at 20:28
  • 6
    @ilkkachu to avoid the heredoc being created at all (at least in modern shells, IIRC in earlier versions of the Bourne shell, the here doc would still be created) Jun 8 at 20:31
1

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.

1
  • Very instructive answer, thanks!
    – k.Cyborg
    Jun 10 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.