I did google this topic and all results are talking about <<EOF
.
But I saw scripts using <<-EOF
, but I can not find anywehre by googling.
Thus, What is different between <<-EOF
and <<EOF
in bash script?
Thanks a lot.
-
1See 3.6.6 Here Documents in the bash manual– glenn jackmanCommented May 1, 2020 at 14:53
2 Answers
<<-EOF
will ignore leading tabs in your heredoc, while <<EOF
will not. Thus:
cat <<EOF
Line 1
Line 2
EOF
will produce
Line 1
Line 2
while
cat <<-EOF
Line 1
Line 2
EOF
produces
Line 1
Line 2
If you use <<-EOF
, I recommend the man page of the Bourne Shell:
If, however, the hyphen (-) is appended to <<:
leading tabs are stripped from word before the shell input is read (but after parameter and command substitution is done on word);
leading tabs are stripped from the shell input as it is read and before each line is compared with word; and
shell input is read up to the first line that literally matches the resulting word, or to an EOF.
So <<-
allows to indent the content of the here document for better readability.