Heredocument Error: Delimited by End of File

bashhere-document

I execute the following code in Bash:

bash /dev/fd/10 10<<-'SES'

    cat <<EMR >> /etc/apache2/apache2.conf
        #
        <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        </Directory>
        EMR

SES

But the execution breaks and I get:

warning: here-document at line 2 delimited by end-of-file (wanted
`EMR')

  • I don't understand what the error means and I already added - between the operator and the name of the containing heredoc to allow tabulations.
  • I made sure there are no spaces after the opener (operator+name) or after the delimiter.

Note: If you copy the code above to test it in your computer, you need to change all leading spaces to tabs (tabulations) as StackExchange changed the original tabulations to spaces.


Possible causes to rule out:

There are several possible causes for this, here are some I've ruled out:

1. Problematic hidden characters:

I edit the file with Notepad++ and I turned on the "Show all symbols" mode to make sure all indents are tabulation based (see red arrows) and that all End Of Line chars (EOLs) are LF unix based (there aren't any CR chars):

enter image description here

Moreover, the encoding is UTF-8, so it's not an encoding problem.

2. Pseudo dash symbol (<<-):

It seems as the added hyphen (<<-) doesn't do its job of stripping all leading tabs because when I manually delete all leading tabs whatsoever (or all leading tabs before each delimiter) the heredoc does work as expected. One can suggest that the hyphen isn't really a dash symbol, but why won't it be? I have no way to validate that.

3. Bash bug:

Other people not-using Windows, and using different Bash-containing Linux distros didn't have this, so this is most likely not a Bash bug. I've also tested this with the Bash development section at GNU.


Corrupted pasting of copied data from Notepad++ might be it:

If I paste the heredoc from Notepad++ into a nano file, it seems the leading tabs are all in place – they aren't deleted are transformed into spaces in pasting.

Moreover, cat script.sh | grep "^ " and cat script.sh | grep -x '\s*EMR' (when done in the files directory), come out empty.

Yet, in a later pasting I found another corruption problem that is most likley to cause this (see my answer).

Best Answer

The image you posted shows carriage returns (CR in the image). This is a DOS-formatted text file. Use dos2unix or some similar utility to convert the file to a proper Unix text file.

As per comment below: If you absolutely need to do development in Notepad++ on Windows (I would personally recommend doing the development on the same OS that you're targeting, if at all possible), then the program may be told to save text files with Unix newlines by selecting "Unix (LF)" in the "New Documents" preferences:

Notepad++ setting for new documents

Make sure that your file has a newline (LF) at the end of the last line. All lines in a Unix text file (and a shell script is a text file) needs a terminating newline at the end. If the last line doesn't have one, the file is technically actually not a text file but binary.

See also, for example, the answers to "Remove ^M character from log files"

Related Question