What is different between "<<-EOF" and "<<EOF" in bash script?

9,225

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 <<:

  1. leading tabs are stripped from word before the shell input is read (but after parameter and command substitution is done on word);

  2. leading tabs are stripped from the shell input as it is read and before each line is compared with word; and

  3. 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.

Share:
9,225

Related videos on Youtube

sgon00
Author by

sgon00

Updated on September 18, 2022

Comments

  • sgon00
    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.