Skip to Content
gnux

ttyx

Print the file name of the terminal connected to standard input.

Mirrors GNU tty. Run it as ttyx, or as gnux ttyx.

Synopsis

ttyx [OPTION]...

GNU reference

Description

ttyx prints the file name of the terminal on standard input, such as /dev/pts/3, or 'not a tty' when standard input is a pipe, a file or nothing, and then exits with status 1. -s prints nothing, so only the exit status tells; GNU deprecates it in favor of 'test -t 0', which gnux has as testx -t 0. The name is found without the C library: through /proc on Linux, and on macOS and the BSDs by finding the device in /dev with the same device number, as ttyname(3) does. Windows has no terminal file names, so a console prints as CON (its device name) instead of failing. The exit status is 0 when standard input is a terminal, 1 when it is not, 2 for a command-line mistake and 3 when the name could not be written.

Permissions

Needs no permissions.

Use cases

  • Making a script behave differently when run by hand than from cron or a pipe.
  • Finding which terminal device a session uses, to write to it or check its permissions.
  • Refusing to prompt for a password when there is no terminal to type it on.
  • Logging which terminal a command was run from.
  • Checking that a CI job really has no terminal attached.

Flags

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

Examples

See which terminal device your shell is using

ttyx
Output
/dev/pts/3

See what a command reading from a pipe gets

echox | ttyx
Output
not a tty

Only prompt for confirmation when someone is at the keyboard

if ttyx -s; then read -p 'Deploy to production? ' ok; else ok=yes; fi

Refuse to ask for a password in a cron job or CI run

ttyx -s || { echox 'no terminal: set DB_PASSWORD instead' >&2; exit 1; }

Record the terminal in an audit log next to the user and time

echox "$(datex -Iseconds) $(whoamix) $(ttyx)" >> audit.log

Check who may write to your terminal (as mesg would)

lsx -l "$(ttyx)"

Show progress only when a person is watching

ttyx -s && echox 'Copying 4.2 GB, this takes a few minutes...'

Confirm a CI job has no terminal attached

ttyx || echox 'running unattended'
Output
not a tty
running unattended

Get the exit status alone, for a test in a script

ttyx -s; echox $?
Output
0

Compare with the portable equivalent GNU recommends

testx -t 0 && echox interactive

Check for a terminal in the portable gnux shell

gnux shell -c 'ttyx -s && echox interactive || echox batch'

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0standard input is a terminal
1standard input is not a terminal
2a command-line mistake
3a write error

Help in the terminal

ttyx --help                # options, first examples, exit status
gnux help ttyx             # this page: the full manual
gnux help ttyx --OPTION    # one option in depth, with its examples

See also

On this page