Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

#!/bin/bash
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
==$BASH
EOL

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

This is the output

# output 1: ==$BASH
# output 2: ==$BASH
# output 3: ==/bin/bash
...: bad substitution: no closing "`" in `my backtick test
share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

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

Try:

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

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

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:
     <<[-]WORD
             HERE-DOCUMENT
     DELIMITER

   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.
share|improve this answer
    
#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 '11 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 '11 at 15:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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