When is useful use "silent" for tty?

If the tty --help command is executed it shows

tty --help
Usage: tty [OPTION]...
Print the file name of the terminal connected to standard input.

  -s, --silent, --quiet   print nothing, only return an exit status
      --help     display this help and exit
      --version  output version information and exit

Therefore when tty -s is executed it returns nothing


  • When is useful use silent for tty?
Asked By: Manuel Jordan


# Script that is run regularly from cron,
# but also sometimes from the command line.

if tty -s; then
    echo 'Running with a TTY available, will output friendly messages'
    echo 'and may be interactive with the user.'

# 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):

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.

Answered By: Kusalananda
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.