0

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

Question

  • When is useful use silent for tty?

1 Answer 1

4
#!/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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .