tty

system administrationLinux/Unix
The tty command is one of the most frequently used commands in Linux/Unix-like operating systems. tty Print the file name of the terminal connected to standard input

Quick Reference

Command Name:

tty

Category:

system administration

Platform:

Linux/Unix

Basic Usage:

tty [options] [arguments]

Common Use Cases

    Syntax

    tty [OPTION]...

    Options

    Option Description
    -s, --silent, --quiet Print nothing, only return an exit status
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the tty command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Print the terminal device name tty
    # Check the exit status without printing the terminal name tty -s && echo "Standard input is a terminal"
    # Advanced Examples Advanced
    # Use in a script to determine if input is from a terminal if tty -s; then echo "Script is running in an interactive terminal" else echo "Script is not running in an interactive terminal" fi
    # Find out which pseudo-terminal a process is connected to ps -o tty,command -p $$
    # Check if a specific terminal is active who | grep $(tty | cut -d/ -f3-) # Use with other commands in a pipeline tty | xargs ls -l # Get all sessions running on the same terminal who | grep $(tty | cut -d/ -f3-) # Verify if the script can request user input if ! tty -s; then echo "Cannot prompt for input: not a terminal" >&2 exit 1 fi # Check terminal access permissions ls -l $(tty) # Use in a conditional command tty -s || echo "This won't show if input is from a terminal" # Determine terminal in a remote session ssh user@host "tty"

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `tty` command is a simple yet useful utility in Unix and Linux systems that prints the filename of the terminal connected to standard input. When run interactively in a terminal, it typically outputs something like `/dev/pts/0` or `/dev/tty1`, indicating which terminal device file is currently being used. This command serves several important purposes, particularly in shell scripting: 1. **Terminal Detection**: The most common use of `tty` is to determine whether a script is being run interactively (with a user at a terminal) or non-interactively (e.g., from a cron job or as part of a pipeline). This is typically done with the `-s` option, which returns an exit status of 0 if standard input is a terminal and a non-zero value otherwise, without producing any output. 2. **Terminal Identification**: System administrators and developers may need to know which specific terminal device a process is using, especially when debugging or when multiple terminal sessions are active. 3. **Permission Checking**: Examining the terminal device file permissions can reveal which users have access to read from or write to your terminal. The term "tty" itself is an abbreviation of "teletype," reflecting the historical origins of Unix terminal interfaces. In modern systems, ttys are typically pseudo-terminals (pts) rather than physical teletype devices. In Unix/Linux, each terminal session is associated with a special file in the `/dev` directory. These files serve as interfaces between the terminal emulator (or physical terminal) and the operating system. When a program reads from or writes to its standard input/output streams, it's actually interacting with these device files. The `tty` command is particularly valuable in shell scripts that need to behave differently based on whether they're receiving input from a user at a terminal or from another program. For example, a script might prompt for user input only when run interactively, or it might use different output formatting when its output is going to a terminal versus being redirected to a file. Despite its simplicity, `tty` is an important part of the Unix/Linux command set and is included in the POSIX standard, ensuring its availability across virtually all Unix-like operating systems.

    Related Commands

    These commands are frequently used alongside tty or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tty command works in different scenarios.

    $ tty
    View All Commands