Skip to content
Advertisement

Cross-platform method to detect whether /dev/tty is available & functional

I have a bash script from which I want to access /dev/tty, but only when it’s available.

When it’s not available (in my case: when running my script in GitHub Actions) then when I try to access it I get /dev/tty: No such device or address, and I’m trying to detect that in advance to avoid the error and provide fallback behaviour instead.

To do so I need a bash test that can detect cleanly this case, and which will work reliably across platforms (i.e. not using the tty command, which has issues on Mac).

I’m currently using [[ -e "/dev/tty" ]] which doesn’t work – it appears to return true even on GitHub Actions, where it seems that /dev/tty exists but accessing it will fail. What should I use instead?

Advertisement

Answer

After testing lots of promising but not quite perfect suggestions (see the other answers), I think I’ve found my own solution that does exactly fit my needs:

if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
    # /dev/tty is available and usable
else
    # /dev/tty is not available
fi

To explain:

: >/dev/tty does nothing (using the : bash built-in) and outputs the nothing to /dev/tty, thereby checking that it exists & it’s writable, but not actually producing any visible output. If this succeeds, we’re good.

If we do that at the top level without a /dev/tty, bash itself produces a noisy error in our output, complaining about /dev/tty being unusable. This can’t be redirected and silenced because it comes from bash itself, not the : command.

Wrapping that with sh -c "..." >/dev/null 2>/dev/null runs the test in a bash subshell, with stdout/stderr removed, and so silences all errors & warnings while still returning the overall exit code.

Suggestions for further improvements welcome. For reference, I’m testing this with setsid <command>, which seems to be a good simulation of the TTY-less environment I’m having trouble with.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement