bash heredoc hangs when more than 512 characters

Viewed 211

my gnu bash scripts GNU bash, version 5.1.0(1)-release (x86_64-apple-darwin19.6.0) on macos hang when they contain a heredoc with greater than 512 characters, e.g. the USAGE heredoc below works unless I add 1 more character to it

cat <<'USAGE'
        --all       List all tasks, TASK_IDs will be ignored
        --name NAME Only list tasks with specified NAME
        --logs      list log messages
        --pending   Only list tasks that have not been scheduled
        --active    same as --pending
        --scheduled Only List tasks that have been scheduled, whether running or finished
        --running   Only List tasks that are currently executing / running
        --finished  Only List tasks that have been run, i.e., have finished
12345678901234567890 
USAGE

note: there are no variable expansions, quotes, etc. just literal text.
If i break all of the text into multiple heredocs they all work ... but if i combine them in anyway to create a heredoc with >512 characters bash hangs what am i doing wrong?

5 Answers

Well - the problem has disappeared. Maybe related to recent bash upgrade to
GNU bash, version 5.1.4(1)-release (x86_64-apple-darwin19.6.0)
which did have changes to heredoc processing related to size of heredoc wrt buffer sizes.

The same thing recently started happening to me. I recognize that this isn't a very satisfying answer, but I just switched back to bash 3.2.57(1)-release, that comes pre-installed with mac. (I spent several hours trying to figure out how to use homebrew to rollback to an earlier version of bash, but, as of 2020, this no longer seems to be a supported feature.)

I ran chsh -s /bin/bash, to change my default shell to the Mac-default bash.

I then then re-arranged my path so that it would find /bin/bash before /usr/local/bin/bash. (If a script has a #!/usr/bin/env bash shebang, it finds the older version.)

I have encounter the same issue. With help of a colleague we trace a quite odd behaviour in one of our tools to this exact issue, when the string to pipe into <<< exceeds 512 characters the command hangs.

We both have the same environment, it works for him it does not for me.

macOS Catalina 10.15.7 Bash installed with brew Bash version GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin19.6.0) Kernel Darwin Kernel Version 19.6.0

When using Mac included Bash it works (GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19) but I cannot use that version.

This is driving us nuts.

Experienced this with bash version 5.1.16(1)-release (x86_64-apple-darwin21.1.0) and fixed via reboot.

This is not really an answer to the question but a suggestion to simplify the code:

echo "
        --all       List all tasks, TASK_IDs will be ignored
        --name NAME Only list tasks with specified NAME
        --logs      list log messages
        --pending   Only list tasks that have not been scheduled
        --active    same as --pending
        --scheduled Only List tasks that have been scheduled, whether running or finished
        --running   Only List tasks that are currently executing / running
        --finished  Only List tasks that have been run, i.e., have finished
12345678901234567890 "

It will do the same but in a easier way.

Related