Version 11 of Terminal

Updated 2019-04-01 18:43:22 by AMG

A terminal is a display unit that is part of the user interface to a computer system. The word carries the connotation of being text-only.

See Also

ANSI
various ANSI standards govern terminal behaviour
xterm
a terminal emulator
curses
a C library for controling terminals
handling of ANSI terminals using Expect
stty
the *nix command to configure and manage a terminal
terminal control sequence

Reference

VT100.net
This site is dedicated to the range of video terminals produced by Digital Equipment Corporation (DEC) from 1970 to 1995. The most famous of these is the VT100.
The TTY demystified , Linus Åkesson, 2008-07-25

Description

Terminal emulators for Linux have support for ANSI escape codes built in.

Checking for a Terminal

AMG: To check if stdin is connected to a terminal, run:

if {![catch {exec tty -s}]} {
    # stdin is a terminal
}

Checking for stdout/stderr is harder because exec does not allow stdout/stderr to be redirected to stdin because stdout/stderr are not considered readable. Instead, let the shell do the redirection. Furthermore, instruct [exec] to not intercept stdout/stderr.

if {![catch {exec /bin/sh -c {tty -s < /dev/stdout}} >@ stdout]} {
    # stdout is a terminal
}
if {![catch {exec /bin/sh -c {tty -s < /dev/stderr}} 2>@ stderr]} {
    # stderr is a terminal
}

Without the -s switch, the tty program not only checks if its stdin is a terminal, but it also prints the device name to its stdout, e.g. /dev/pts/0.