bashshellposixheredoc

How to make a command stop expecting input if heredoc delimiter is ""?


In a POSIX shell (let's consider bash in this example), whenever we run cmd << eof, the shell expects input until a string containing only eof is received. Also, the standard states that:

If any part of word [delimiter] is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself.

So, in an exercise of curiosity, I tried running the following command: cat << "" aaaaand I cannot make the shell stop waiting for input. I imagine that the quotes were removed and, as nothing is left, nothing in fact is the delimiter of the heredoc.

I have tried sending the following strings: ", "", \0, etc.

The only way to get out, is, as far as I know it, through a signal that terminates the process.

My question is: is there a way to 'properly' tell the shell to stop expecting input in this situation?


Solution

  • cat << "" (contrary to many syntax-highlighting routines) starts a "paragraph" heredoc; the first empty line terminates the heredoc:

    cat << ""
    lines here
    will be
    piped to
    cat
    $this_variable_does_not_expand
    `neither does this`
    
    # Back to the script
    ls -la
    

    In my coding habits, I always use a delimiter.

    If you've typed cat << "" in your interactive shell, you can halt it by typing Ctrl-D or pressing enter twice.