Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'tty' Command In Linux

2023-06-20 - By Robert Elder

     I use the 'tty' command to identify the terminal file that's connected to standard input.  If I run the 'tty' command on my desktop graphical terminal:

tty

     I'll see this:

/dev/pts/0

     which indicates a pseudo-terminal.

Pseudo-terminals Vs. Terminals

     If I open a terminal emulator like GNU screen or tmux:

screen

     and then run the 'tty' command in this terminal emulator, I'll see another pseudo-terminal reference:

tty
/dev/pts/1

     If I switch out of a graphical environment, into a console (by holding CTRL + ALT + one of the F1, F2, F3, ... keys), I'll see a terminal file that looks like this:

tty
/dev/tty3

Return Codes

     The return code from the tty command also provides meaningful information, as illustrated by this wrapper script:

#!/bin/bash

if [ "${1}" == "do-close-stdout" ]; then
	#  Runs tty, but with closed stdout file descriptor:
	CMD_TO_EXECUTE="tty >&-"
elif [ "${1}" == "do-bad-arguments" ]; then
	#  Runs tty, but with incorrect number of arguments:
	CMD_TO_EXECUTE="tty 1 2 3 4 5"
else
	#  Runs tty as normal
	CMD_TO_EXECUTE="tty"
fi

echo "About to run this command: '${CMD_TO_EXECUTE}'"
TTY_OUTPUT=$(bash -c "${CMD_TO_EXECUTE}")
TTY_RETURN_CODE=$?

if [ "${TTY_RETURN_CODE}" -eq 0 ]; then
     echo "Case 0: tty is '${TTY_OUTPUT}' and rtn code was ${TTY_RETURN_CODE}: Standard input is a terminal."
elif [ "${TTY_RETURN_CODE}" -eq 1 ]; then
     echo "Case 1: tty is '${TTY_OUTPUT}' and rtn code was ${TTY_RETURN_CODE}: Standard input is a non-terminal file."
elif [ "${TTY_RETURN_CODE}" -eq 2 ]; then
     echo "Case 2: tty is '${TTY_OUTPUT}' and rtn code was ${TTY_RETURN_CODE}: Given incorrect arguments."
elif [ "${TTY_RETURN_CODE}" -eq 3 ]; then
     echo "Case 3: tty is '${TTY_OUTPUT}' and rtn code was ${TTY_RETURN_CODE}: A write error occurred."
else
     echo "else Case: tty is '${TTY_OUTPUT}' and rtn code was ${TTY_RETURN_CODE}: Unknown rtn code???"
fi

     The return code has a value of 0 when standard input is a terminal:

Return Code 0

./tty-command-wrapper.sh
About to run this command: 'tty'
Case 0: tty is '/dev/pts/0' and rtn code was 0: Standard input is a terminal.

Return Code 1

     A value of 1 is returned when standard input is a non-terminal file:

touch file1.txt
./tty-command-wrapper.sh < file1.txt
About to run this command: 'tty'
Case 1: tty is 'not a tty' and rtn code was 1: Standard input is a non-terminal file.

Return Code 2

     When the arguments to tty are incorrect, the return value is 2:

./tty-command-wrapper.sh "do-bad-arguments"
About to run this command: 'tty 1 2 3 4 5'
tty: extra operand ‘1’
Try 'tty --help' for more information.
Case 2: tty is '' and rtn code was 2: Given incorrect arguments.

Return Code 3

     If a write error occurs the return value is 3:

./tty-command-wrapper.sh "do-close-stdout"
About to run this command: 'tty >&-'
tty: write error: Bad file descriptor
Case 3: tty is '' and rtn code was 3: A write error occurred.

     In this case, the write error was triggered by using this special syntax to close standard out so that write operations fail:

>&-

     And that's why the 'tty' command is my faviourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use