tty Command Cheat Sheet
tty prints the file name of the terminal connected to standard input.
Synopsis
tty [OPTION]...
Basic Usage
tty
# Output: /dev/pts/0
If run from a script without a terminal (e.g., cron):
tty
# Output: not a tty
Silent Mode (-s)
Do not print anything, just return an exit status. - 0: Standard input is a terminal. - 1: Standard input is NOT a terminal.
if tty -s; then
echo "Running interactively"
else
echo "Running in background/script"
fi
Notes
- SSH: When you SSH in, you are assigned a pseudo-terminal (
/dev/pts/N). - Console: Physical console is usually
/dev/tty1.