#!/bin/sh
# Script that is run regularly from cron,
# but also sometimes from the command line.
interactive=false
if tty -s; then
echo 'Running with a TTY available, will output friendly messages'
echo 'and may be interactive with the user.'
interactive=true
fi
# etc.
In short, it provides a way of testing whether a TTY is attached to the current shell session's standard input stream, which indicates that the script may be able to interact with a user by reading from the standard input stream. You can also do this using the test [ -t 0 ]
, or the equivalent test -t 0
, which is true if fd 0 (standard input) is a TTY.
The -s
option and its variations are non-standard (not part of the POSIX specification of tty
), and the OpenBSD manual for tty
also mentions the -t
test (which is standard):
-s
Don't write the terminal name; only the exit status is affected
when this option is specified. The -s option is deprecated in
favor of the “test -t 0” command.