The Mystery of Multiple Terminals
In modern Linux environments, especially when managing remote servers or working on complex development tasks, you rarely have just one terminal open. You might have three SSH sessions connected to the same server, a local terminal window, and two background sessions running inside a terminal multiplexer like tmux or screen.
If you need to send a message to a specific terminal, terminate a frozen session, or write a script that behaves differently depending on whether it is running directly on the physical console or over a remote SSH connection, you need to know exactly which terminal you are currently typing in.
You can instantly identify your current terminal session using the tty (Teletypewriter) command.
Step 1: Understanding TTY vs. PTS
The term “TTY” is a historical holdover from the 1970s, referring to physical electro-mechanical “Teletype” machines used to send commands to early mainframe computers.
Today, Linux categorizes terminal interfaces into two main types:
- TTY (Teletype): A direct, physical console connection. If you plug a monitor and keyboard directly into a Linux server and log in, you are using a true TTY (often labeled
tty1,tty2, etc.). - PTS (Pseudo-Terminal Slave): An emulated terminal. Every time you open a terminal emulator app on your desktop (like GNOME Terminal), connect via SSH, or open a tab in
tmux, Linux creates a virtual “pseudo-terminal” (labeledpts/0,pts/1, etc.).
Step 2: Using the tty Command
To find out the exact device name of your current terminal session, simply type the command:
tty
If you are connected via SSH or using a desktop terminal application, the output will look something like this:
/dev/pts/1
This tells you that your current session is Pseudo-Terminal number 1, and its device file is located in the /dev/pts/ directory.
Step 3: Practical Use: Sending Messages to Other Terminals
Because in Linux “everything is a file,” terminal sessions are represented as files in the /dev/ directory. This means you can write data directly to another user’s terminal screen.
Imagine you are logged in via SSH as pts/1, and your colleague is logged into the same server via SSH as pts/2. You need to tell them you are rebooting the server.
You can use the echo command and redirect the output directly to their terminal device:
echo "Warning: Rebooting the server in 5 minutes!" > /dev/pts/2
The message will instantly appear in the middle of their command line, regardless of what they are currently typing.
Step 4: Using tty in Bash Scripts
The tty command is frequently used in Bash scripting to determine if a script is being run interactively by a human, or silently in the background by a cron job.
By default, tty returns an exit status of 0 if standard input is a terminal, and an exit status of 1 if it is not (and prints “not a tty”).
You can use the -s (silent) flag in a script to suppress the text output and only return the exit code for a logical test:
if tty -s; then
echo "You are running this in a terminal."
# Prompt the user for input
else
echo "This is running as a background task."
# Automatically apply default values
fi
This ensures your scripts don’t hang indefinitely waiting for user input when they are executed by a background scheduler that has no terminal attached to it.