What is different between "<<-EOF" and "<<EOF" in bash script?
Solution 1
<<-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
Solution 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.
Related videos on Youtube
sgon00
Updated on September 18, 2022Comments
-
sgon00 over 1 year
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.-
Angel Todorov about 4 yearsSee 3.6.6 Here Documents in the bash manual
-