← 返回命令列表

Linux command

tty 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Print the terminal device

tty

Check silently

tty -s

Use in a script

if tty -s; then echo "Interactive"; fi

Show version

tty --version

说明

tty prints the file name of the terminal connected to standard input. The output is typically a device path like /dev/pts/0 (pseudo-terminal) or /dev/tty1 (virtual console). If standard input is not connected to a terminal (e.g., when input is piped or redirected from a file), tty prints "not a tty" and exits with status 1. The -s option suppresses output entirely, making it useful in scripts where only the exit status matters. This allows testing whether a script is running interactively or in a batch/piped context.

参数

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

FAQ

What is the tty command used for?

tty prints the file name of the terminal connected to standard input. The output is typically a device path like /dev/pts/0 (pseudo-terminal) or /dev/tty1 (virtual console). If standard input is not connected to a terminal (e.g., when input is piped or redirected from a file), tty prints "not a tty" and exits with status 1. The -s option suppresses output entirely, making it useful in scripts where only the exit status matters. This allows testing whether a script is running interactively or in a batch/piped context.

How do I run a basic tty example?

Run `tty` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -s, --silent, --quiet do in tty?

Print nothing; only return exit status