2

From an answer to "Bash script doesn't see SIGHUP?", I've got the following in my script:

while true; do read; done

And, usually, this works fine. However, when run from (tl;dr) ... something else, read exits with status code 1.

Why? And how can I deal with this and restore the desired behaviour (correctly triggering the EXIT trap)?


(The "something else" is GNU make running Erlang's ct_run, which uses erlexec to run the script)

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1

The read documentation in the bash man page says

The exit status is zero, unless end-of-file is encountered, read times out (in which case the status is greater than 128), a variable assignment error (such as assigning to a readonly variable) occurs, or an invalid file descriptor is supplied as the argument to -u.

In your case I suspect standard input is reaching end-of-file.

Since you’re trying to wait indefinitely while still allowing CtrlC, perhaps the following would work better:

while sleep 1; do :; done

This avoids surprises related to I/O handling and ensures a timely reaction to signals.

Stephen Kitt
  • 434,908