Aside from the variable expansion mentioned in the question's heading, I also experience another alarming issue when reading inline data to <<EOL... When the data contains a backtick `, it causes an error.

Both are substitution issues, but what and where is the relevant option? ...(and why is the inline read behaving differently?)

Here is the script

echo '==$BASH' | \
while IFS= read -r line ; do echo "# output 1: $line" ;done

echo '==$BASH'>junk
while IFS= read -r line ; do echo "# output 2: $line" ;done <junk

while IFS= read -r line ; do echo "# output 3: $line" ;done <<EOL

while IFS= read -r line ; do echo "# output 4: $line" ;done <<EOL
`my backtick test

This is the output

# output 1: ==$BASH
# output 2: ==$BASH
# output 3: ==/bin/bash
...: bad substitution: no closing "`" in `my backtick test

1 Answer 1


Shell expansion is one of the main benefits of 'here documents'. Good news is you can turn it off.


while IFS= read -r line ; do echo "# output 3: $line" ;done <<'EOL'

while IFS= read -r line ; do echo "# output 4: $line" ;done <<'EOL'
`my backtick test

For details see:

> info bash


3.6.6 Here Documents

This type of redirection instructs the shell to read input from the
current source until a line containing only WORD (with no trailing
blanks) is seen.  All of the lines read up to that point are then used
as the standard input for a command.

   The format of here-documents is:

   No parameter expansion, command substitution, arithmetic expansion,
or filename expansion is performed on WORD.  If any characters in WORD
are quoted, the DELIMITER is the result of quote removal on WORD, and
the lines in the here-document are not expanded.  If WORD is unquoted,
all lines of the here-document are subjected to parameter expansion,
command substitution, and arithmetic expansion.  In the latter case,
the character sequence `\newline' is ignored, and `\' must be used to
quote the characters `\', `$', and ``'.

   If the redirection operator is `<<-', then all leading tab
characters are stripped from input lines and the line containing
DELIMITER.  This allows here-documents within shell scripts to be
indented in a natural fashion.
  • #asoundmove: Thanks, that's fantastic... and I like the [-] ability to allow indentation. That has bugged me, and to find a way around it is a nice bonus :) .. but I'll have to watch out that the tabs don't get changed to spaces.. (as I have my editor set to change tabs to spaces when I type a tab.. but I can paste a tab as a tab (but If I ever retab without thinking, it could cause "unpredictable results".... but it will be handy for testing ...
    – Peter.O
    Apr 16, 2011 at 8:47
  • @fred.bear, you're most welcome. Retabbing will be an issue with any editor, but for scripting (shell, awk, perl, sed, grep and general text parsing utilities) it is not a good idea to have your editor replace tabs with spaces as for text parsing tabs are often used as a delimiter.
    – asoundmove
    Apr 16, 2011 at 15:29
  • 1
    Nice tips. I have just tested and can confirm that both <<- and 'EOF' also work in zsh, posh, ksh, dash and ash. Feb 17, 2016 at 8:10

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.