Bash shell read error: 0: Resource temporarily unavailable

I had a similar issue, but the command I was running did need a real STDIN, /dev/null wasn't good enough. Instead, I was able to do:

TTY=$(/usr/bin/tty)
cmd-using-stdin < $TTY
read -r var

or combined with spbnick's answer:

TTY=$(/usr/bin/tty)
exec 3<&0 < $TTY
cmd-using-stdin
read -r var 0<&3`

which leaves a clean STDIN in 3 for you to read and 0 becomes a fresh stream from the terminal for the command.


I had the same problem. I solved by reading directly from tty like this, redirecting stdin:

read -p "Play both [y]? " -n 1 -r </dev/tty

instead of simply:

read -p "Play both [y]? " -n 1 -r

In my case, the use of exec 3<&0 ... didn't work.


Usually it is important to know what input the invoked program expects and from where, so it is not a problem to redirect stdin from /dev/null for those that shouldn't be getting any.

Still, it is possible to do it for the shell itself and all invoked programs. Simply move stdin to another file descriptor and open /dev/null in its place. Like this:

exec 3<&0 0</dev/null

The above duplicates stdin file descriptor (0) under file descriptor 3 and then opens /dev/null to replace it.

After this any invoked command attempting to read stdin will be reading from /dev/null. Programs that should read original stdin should have redirection from file descriptor 3. Like this:

read -r var 0<&3

The < redirection operator assumes destination file descriptor 0, if it is omitted, so the above two commands could be written as such:

exec 3<&0 </dev/null
read -r var <&3

When this happens, run bash from within your bash shell, then exit it (thus returning to the original bash shell). I found a mention of this trick in https://github.com/fish-shell/fish-shell/issues/176 and it worked for me, seems like bash restores the STDIN state. Example:

bash> do something that exhibits the STDIN problem
bash> bash
bash> exit
bash> repeat something: STDIN problem fixed

Tags:

Shell

Bash