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):
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).
<<-
should eat all leading tabs and the inner one shouldn't see any – ilkkachu Apr 19 '17 at 11:46EMR
, but what it sees, isEMR\r
. – ilkkachu Apr 19 '17 at 18:05cr
is necessarily a "Carriage return" or even not part of an EOL of some kind, nor did I ever expected that Notepad++ (which is the most usable text editor I know in the Windows world) would include these for files with the.sh
extension. I believe I would further investigate this if there weren't answers that fast and learn it (BTW, I found these chars only after turning on the hidden character display - In Notepad++ it's under View >> Show symbol >> Show all characters). – Apr 19 '17 at 18:21source filename
; 3. if it doesn't work, provide the output ofod -c filename
– derobert Apr 21 '17 at 15:28#!/bin/bash
) in this case the script works only partially. Did you mean that? – Apr 21 '17 at 15:34cat > filename
, then paste, then type control-D (if a prompt doesn't appear after control-D, try a second control-D). That should write whatever you pasted tofilename
. You can then immediately trysource filename
to run it. – derobert Apr 21 '17 at 15:37od -c script.sh
to see if anything funny got in there). – derobert Apr 21 '17 at 15:45cat > filename [Paste content and click CTRL+D] && source filename
, in both cases it did worked. That's really interesting @derobert. – Apr 21 '17 at 15:55cat > filename [Paste content and click CTRL+D] && source filename
) to run the heredoc without such random corruption? Please publish an answer with that, not only it's a milestone, it's really the basis for a working answer. – Apr 21 '17 at 16:09^A^K
deletes the line you so carefully entered. An interactive shell will have features meant for interactive users, not for entering arbitrary data. There are some ways to detect pasted data from typed data, but they require support from the application and possibly the terminal. In general it's better to just not use an interactive shell if you don't want the interactive features. – ilkkachu Apr 23 '17 at 10:27