How to Use the Linux tty Command to Print Your Terminal Name

In the world of Linux and Unix, “everything is a file.” This core philosophy means that your keyboard, your mouse, your hard drive, and even the terminal window you are currently typing in are all represented as text files located somewhere in the /dev/ (devices) directory. When you open a terminal emulator (like GNOME Terminal) or connect to a server via SSH, the operating system assigns a unique virtual terminal file to that specific session. If you are writing a complex bash script that needs to interact directly with the user’s screen—or if you are a system administrator trying to figure out which terminal session a rogue background process is attached to—you need to know the name of this file. The Linux tty command serves exactly this purpose.

What Does “tty” Mean?

The name tty is a historical artifact. It stands for Teletypewriter. In the 1970s, before computer monitors were common, programmers interacted with mainframe computers by typing on mechanical typewriter keyboards, and the computer’s output was printed onto physical rolls of paper. Today, we use software to emulate these mechanical terminals, which is why modern terminal names usually begin with pts (pseudo-terminal slave) rather than the physical hardware designation tty.

Executing the tty Command

Using the command is as simple as it gets; it requires no flags or arguments for basic operation.

  1. Open your terminal or SSH into your server.
  2. Type the command: tty
  3. Press Enter.

The output will be a single line showing the absolute path to the file representing your current standard input device. For example, if you are using a graphical desktop environment in Ubuntu, the output will likely look like this:

/dev/pts/0

If you open a second terminal window right next to the first one and run the command again, it will output a different number (e.g., /dev/pts/1), proving that every window is treated as a separate, distinct device.

Practical Uses for the tty Command

While printing the terminal name to the screen is interesting trivia, the tty command is primarily used programmatically within bash scripts.

Checking for Interactive Sessions (The -s Flag)

If you write a script that requires a user to type a password, that script will fail if it is executed by an automated cron job in the background, because a cron job has no terminal attached to it to prompt the user. You can use the tty command with the -s (silent) flag to test if a real human is sitting at a terminal.

if tty -s; then
    echo "Terminal detected. Please enter your password:"
    read password
else
    echo "Error: This script must be run interactively in a terminal."
    exit 1
fi

The -s flag prevents tty from printing the /dev/pts/0 path to the screen; it simply returns an exit status of 0 if a terminal exists, and 1 if it does not.

Sending Messages to Other Terminals

Because the terminal is just a file, you can write text directly to it using standard redirection. If you run the w command to list all logged-in users and you see that your coworker “John” is logged into /dev/pts/2, you can send him a message directly onto his screen by echoing text into his terminal file (assuming you have root or appropriate write permissions):

echo "Hey John, rebooting the server in 5 minutes" > /dev/pts/2

The message will immediately print on John’s screen, interrupting whatever command he was currently typing.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.