I have a misbehaving program that I need to call from my script. It exits as soon as it sees something on stdin. Is there a way to close stdin?
Is there a way to close stdin?
Closing File Descriptors
n<&-Close input file descriptor n.
Source Chapter 20. I/O Redirection
I found myself struggling with the same problem. My solution is derived from the suggestions here, but differ in that instead of closing
exec 0<&- which allows for subshells to continuously use stdin. An example program that you would expect to exit but does not would look like:
exec 0<&- $(read stdin)
This is because
read is spawned inside a subshell inheriting
stdin from the parent.
If we instead redirect the
stdin we get the expected behaviour
exec 0</dev/null $(read stdin)
This will yield an exit code 1.