国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > stty 以获得更好的体验。

# Bash 的 stty 命令

stty [OPTION]... [SETTING]...

!subtitle:功能

查看或修改终端配置。

!subtitle:类型

可执行文件(/usr/bin/stty),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -a, --all - 以易于阅读的形式打印所有当前终端设置

    • -g, --save - 以紧凑格式打印所有当前终端设置

    • -F, --file=DEVICE - 指定终端设备

    • --help - 显示帮助

    • --version - 显示版本

  • SETTING - 设置项列表

# 设置项说明

特殊字符 说明 备注
discard CHAR CHAR 将切换(开启/关闭)输出丢弃功能 非 POSIX 标准
eof CHAR CHAR 将发送文件结束符(终止输入)
eol CHAR CHAR 将结束当前行
eol2 CHAR 用于结束行的备用 CHAR 非 POSIX 标准
erase CHAR CHAR 将删除最后输入的一个字符
intr CHAR CHAR 将发送中断信号
kill CHAR CHAR 将删除当前整行
lnext CHAR CHAR 将以字面值方式输入下一字符(引用输入) 非 POSIX 标准
quit CHAR CHAR 将发送退出(quit)信号
rprnt CHAR CHAR 将重新绘制当前行 非 POSIX 标准
start CHAR CHAR 将在停止后恢复输出
stop CHAR CHAR 将停止输出
susp CHAR CHAR 将发送终端挂起(stop)信号
swtch CHAR CHAR 将切换到另一个 shell 层 非 POSIX 标准
werase CHAR CHAR 将删除上一个单词 非 POSIX 标准
特殊设置 说明 备注
N 将输入和输出速度设置为 N 波特率
cols N 告诉内核终端宽度为 N 列 非 POSIX 标准
columns N 等同于 cols N 非 POSIX 标准
[-]drain 在应用新设置前等待传输完成(默认启用) 非 POSIX 标准
ispeed N 将输入速度设置为 N
line N 使用编号为 N 的 line discipline(行规程) 非 POSIX 标准
min N 在使用 -icanon 时,设置读取完成所需的最少字符数
ospeed N 将输出速度设置为 N
rows N 告诉内核终端高度为 N 行 非 POSIX 标准
size 打印内核记录的终端行数与列数 非 POSIX 标准
speed 打印终端当前速度
time N 在使用 -icanon 时,设置读取超时时间(N × 0.1 秒)
控制设置 说明 备注
[-]clocal 禁用或启用调制解调器控制信号(- 为禁用,默认依系统而定)
[-]cread 允许或禁止接收输入
[-]crtscts 启用或关闭 RTS/CTS 硬件握手流控 非 POSIX 标准
csN 设置字符位宽为 N 位(N 取值范围 5~8)
[-]cstopb 每个字符使用两个停止位(加 - 时为使用一个停止位)
[-]hup 当最后一个进程关闭 TTY 时发送挂断信号
[-]hupcl 等同于 [-]hup
[-]parenb 输出中生成奇偶校验位,并在输入时要求奇偶校验位
[-]parodd 使用奇校验(加 - 时使用偶校验)
[-]cmspar 使用 "stick" 校验方式(mark/space parity) 非 POSIX 标准
输入设置 说明 备注
[-]brkint break 字符会触发中断信号
[-]icrnl 将回车符(CR)转换为换行符(NL)
[-]ignbrk 忽略 break 字符
[-]igncr 忽略回车符(CR)
[-]ignpar 忽略带奇偶校验错误的字符
[-]imaxbel 当输入缓冲区满时发出响铃(不清空缓冲区) 非 POSIX 标准
[-]inlcr 将换行符(NL)转换为回车符(CR)
[-]inpck 启用输入奇偶校验检查
[-]istrip 清除输入字符的第 8 位(只保留低 7 位)
[-]iutf8 假定输入字符是 UTF-8 编码 非 POSIX 标准
[-]iuclc 将大写字符转换为小写 非 POSIX 标准
[-]ixany 输入任意字符都可重新启动输出(不只限 start 字符) 非 POSIX 标准
[-]ixoff 启用发送 XOFF/XON 的控制字符(软件流控)
[-]ixon 启用接收 XON/XOFF 流控
[-]parmrk 用 255-0-字符 的序列标记奇偶校验错误
[-]tandem 等同于 [-]ixoff
输出设置 说明 备注
bsN 退格(Backspace)延时样式,N 取值范围 0~1 非 POSIX 标准
crN 回车(Carriage Return)延时样式,N 取值范围 0~3 非 POSIX 标准
ffN 换页(Form Feed)延时样式,N 取值范围 0~1 非 POSIX 标准
nlN 换行(Newline)延时样式,N 取值范围 0~1 非 POSIX 标准
[-]ocrnl 将回车符(CR)转换为换行符(NL) 非 POSIX 标准
[-]ofdel 填充字符使用 DEL(0x7F),而不是 NUL(0x00) 非 POSIX 标准
[-]ofill 使用填充字符(padding)而不是基于时间的延迟 非 POSIX 标准
[-]olcuc 将小写字母转换为大写字母 非 POSIX 标准
[-]onlcr 将换行符(NL)转换成回车-换行(CR-NL) 非 POSIX 标准
[-]onlret 换行符(NL)同时执行回车(CR)动作 非 POSIX 标准
[-]onocr 在当前列为第一列时不输出回车符(CR) 非 POSIX 标准
[-]opost 启用或禁用输出后处理
tabN 水平制表符(tab)延时样式,N 取值范围 0~3 非 POSIX 标准
tabs 等同于 tab0 非 POSIX 标准
-tabs 等同于 tab3 非 POSIX 标准
vtN 垂直制表符(vertical tab)延时样式,N 取值范围 0~1 非 POSIX 标准
本地设置 说明 备注
[-]crterase 将删除字符以“退格-空格-退格”的方式回显
crtkill 按照 echoprtechoe 的设置删除(kill)整行 非 POSIX 标准
-crtkill 按照 echoctlechok 的设置删除(kill)整行 非 POSIX 标准
[-]ctlecho 以帽号表示法(如 ^C)回显控制字符 非 POSIX 标准
[-]echo 回显输入字符
[-]echoctl 等同于 [-]ctlecho 非 POSIX 标准
[-]echoe 等同于 [-]crterase
[-]echok 在 kill 字符后回显一个换行符
[-]echoke 等同于 [-]crtkill 非 POSIX 标准
[-]echonl 即使不回显其他字符,也回显换行符
[-]echoprt 以反向方式回显被删除的字符,并包围在 \/ 之间 非 POSIX 标准
[-]extproc 启用 LINEMODE(适用于高延迟链接) 非 POSIX 标准
[-]flusho 丢弃正在输出的数据 非 POSIX 标准
[-]icanon 启用特殊字符:erase,kill,werase,rprnt
[-]iexten 启用非 POSIX 的扩展特殊字符
[-]isig 启用中断,退出与挂起(suspend)特殊字符
[-]noflsh 禁止在中断或退出字符后刷新缓冲区
[-]prterase 等同于 [-]echoprt 非 POSIX 标准
[-]tostop 阻止后台任务向终端写入(会发送 SIGTTOU) 非 POSIX 标准
[-]xcase 在 canonical 模式下,对大写字母加 \ 进行转义 非 POSIX 标准
组合设置 说明 备注
[-]LCASE 等同于 [-]lcase 非 POSIX 标准
cbreak 等同于 -icanon(关闭规范模式,单字符输入)
-cbreak 等同于 icanon(开启规范模式)
cooked 将 brkint ignpar istrip icrnl ixon opost isig icanon eof 和 eol 字符恢复默认值
-cooked 等同于 raw(关闭规范模式并应用 raw 设置)
crt 等同于 echoe echoctl echoke
dec 等同于 echoe echoctl echoke -ixany intr ^C erase 0177 kill ^U
[-]decctlq 等同于 [-]ixany 非 POSIX 标准
ek 将 erase 和 kill 字符恢复默认值
evenp 等同于 parenb -parodd cs7(偶校验,7 位字符)
-evenp 等同于 -parenb cs8(关闭奇偶校验,8 位字符)
[-]lcase 等同于 xcase iuclc olcuc(小写字母处理相关) 非 POSIX 标准
litout 等同于 -parenb -istrip -opost cs8
-litout 等同于 parenb istrip opost cs7
nl 等同于 -icrnl -onlcr
-nl 等同于 icrnl -inlcr -igncr onlcr -ocrnl -onlret
oddp 等同于 parenb parodd cs7(奇校验,7 位字符)
-oddp 等同于 -parenb cs8(关闭奇偶校验,8 位字符)
[-]parity 等同于 [-]evenp
pass8 等同于 -parenb -istrip cs8(8 位字符,无奇偶校验)
-pass8 等同于 parenb istrip cs7
raw 等同于关闭多种选项:-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -icanon -opost -isig -iuclc -ixany -imaxbel -xcase min 1 time 0
-raw 等同于 cooked
sane 等同于开启 cread -ignbrk brkint -inlcr -igncr icrnl icanon iexten echo echoe echok -echonl -noflsh -ixoff -iutf8 -iuclc -ixany imaxbel -xcase -olcuc -ocrnl opost -ofill onlcr -onocr -onlret nl0 cr0 tab0 bs0 vt0 ff0 isig -tostop -ofdel -echoprt echoctl echoke -extproc -flusho,以及所有特殊字符恢复默认值

# 示例

$ stty -a       # 易于阅读的终端设置
speed 38400 baud; rows 55; columns 230; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

# 推荐阅读

# 手册

STTY(1)                          User Commands                         STTY(1)

NAME
       stty - change and print terminal line settings

SYNOPSIS
       stty [-F DEVICE | --file=DEVICE] [SETTING]...
       stty [-F DEVICE | --file=DEVICE] [-a|--all]
       stty [-F DEVICE | --file=DEVICE] [-g|--save]

DESCRIPTION
       Print or change terminal characteristics.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -a, --all
              print all current settings in human-readable form

       -g, --save
              print all current settings in a stty-readable form

       -F, --file=DEVICE
              open and use the specified DEVICE instead of stdin

       --help display this help and exit

       --version
              output version information and exit

       Optional - before SETTING indicates negation.   An  *  marks  non-POSIX
       settings.  The underlying system defines which settings are available.

   Special characters:
       * discard CHAR
              CHAR will toggle discarding of output

       eof CHAR
              CHAR will send an end of file (terminate the input)

       eol CHAR
              CHAR will end the line

       * eol2 CHAR
              alternate CHAR for ending the line

       erase CHAR
              CHAR will erase the last character typed

       intr CHAR
              CHAR will send an interrupt signal

       kill CHAR
              CHAR will erase the current line

       * lnext CHAR
              CHAR will enter the next character quoted

       quit CHAR
              CHAR will send a quit signal

       * rprnt CHAR
              CHAR will redraw the current line

       start CHAR
              CHAR will restart the output after stopping it

       stop CHAR
              CHAR will stop the output

       susp CHAR
              CHAR will send a terminal stop signal

       * swtch CHAR
              CHAR will switch to a different shell layer

       * werase CHAR
              CHAR will erase the last word typed

   Special settings:
       N      set the input and output speeds to N bauds

       * cols N
              tell the kernel that the terminal has N columns

       * columns N
              same as cols N

       * [-]drain
              wait for transmission before applying settings (on by default)

       ispeed N
              set the input speed to N

       * line N
              use line discipline N

       min N  with -icanon, set N characters minimum for a completed read

       ospeed N
              set the output speed to N

       * rows N
              tell the kernel that the terminal has N rows

       * size print the number of rows and columns according to the kernel

       speed  print the terminal speed

       time N with -icanon, set read timeout of N tenths of a second

   Control settings:
       [-]clocal
              disable modem control signals

       [-]cread
              allow input to be received

       * [-]crtscts
              enable RTS/CTS handshaking

       csN    set character size to N bits, N in [5..8]

       [-]cstopb
              use two stop bits per character (one with '-')

       [-]hup send a hangup signal when the last process closes the tty

       [-]hupcl
              same as [-]hup

       [-]parenb
              generate parity bit in output and expect parity bit in input

       [-]parodd
              set odd parity (or even parity with '-')

       * [-]cmspar
              use "stick" (mark/space) parity

   Input settings:
       [-]brkint
              breaks cause an interrupt signal

       [-]icrnl
              translate carriage return to newline

       [-]ignbrk
              ignore break characters

       [-]igncr
              ignore carriage return

       [-]ignpar
              ignore characters with parity errors

       * [-]imaxbel
              beep and do not flush a full input buffer on a character

       [-]inlcr
              translate newline to carriage return

       [-]inpck
              enable input parity checking

       [-]istrip
              clear high (8th) bit of input characters

       * [-]iutf8
              assume input characters are UTF-8 encoded

       * [-]iuclc
              translate uppercase characters to lowercase

       * [-]ixany
              let any character restart output, not only start character

       [-]ixoff
              enable sending of start/stop characters

       [-]ixon
              enable XON/XOFF flow control

       [-]parmrk
              mark parity errors (with a 255-0-character sequence)

       [-]tandem
              same as [-]ixoff

   Output settings:
       * bsN  backspace delay style, N in [0..1]

       * crN  carriage return delay style, N in [0..3]

       * ffN  form feed delay style, N in [0..1]

       * nlN  newline delay style, N in [0..1]

       * [-]ocrnl
              translate carriage return to newline

       * [-]ofdel
              use delete characters for fill instead of NUL characters

       * [-]ofill
              use fill (padding) characters instead of timing for delays

       * [-]olcuc
              translate lowercase characters to uppercase

       * [-]onlcr
              translate newline to carriage return-newline

       * [-]onlret
              newline performs a carriage return

       * [-]onocr
              do not print carriage returns in the first column

       [-]opost
              postprocess output

       * tabN horizontal tab delay style, N in [0..3]

       * tabs same as tab0

       * -tabs
              same as tab3

       * vtN  vertical tab delay style, N in [0..1]

   Local settings:
       [-]crterase
              echo erase characters as backspace-space-backspace

       * crtkill
              kill all line by obeying the echoprt and echoe settings

       * -crtkill
              kill all line by obeying the echoctl and echok settings

       * [-]ctlecho
              echo control characters in hat notation ('^c')

       [-]echo
              echo input characters

       * [-]echoctl
              same as [-]ctlecho

       [-]echoe
              same as [-]crterase

       [-]echok
              echo a newline after a kill character

       * [-]echoke
              same as [-]crtkill

       [-]echonl
              echo newline even if not echoing other characters

       * [-]echoprt
              echo erased characters backward, between '\' and '/'

       * [-]extproc
              enable "LINEMODE"; useful with high latency links

       * [-]flusho
              discard output

       [-]icanon
              enable special characters: erase, kill, werase, rprnt

       [-]iexten
              enable non-POSIX special characters

       [-]isig
              enable interrupt, quit, and suspend special characters

       [-]noflsh
              disable flushing after interrupt and quit special characters

       * [-]prterase
              same as [-]echoprt

       * [-]tostop
              stop background jobs that try to write to the terminal

       * [-]xcase
              with icanon, escape with '\' for uppercase characters

   Combination settings:
       * [-]LCASE
              same as [-]lcase

       cbreak same as -icanon

       -cbreak
              same as icanon

       cooked same  as  brkint ignpar istrip icrnl ixon opost isig icanon, eof
              and eol characters to their default values

       -cooked
              same as raw

       crt    same as echoe echoctl echoke

       dec    same as echoe echoctl echoke -ixany intr ^c erase 0177 kill ^u

       * [-]decctlq
              same as [-]ixany

       ek     erase and kill characters to their default values

       evenp  same as parenb -parodd cs7

       -evenp same as -parenb cs8

       * [-]lcase
              same as xcase iuclc olcuc

       litout same as -parenb -istrip -opost cs8

       -litout
              same as parenb istrip opost cs7

       nl     same as -icrnl -onlcr

       -nl    same as icrnl -inlcr -igncr onlcr -ocrnl -onlret

       oddp   same as parenb parodd cs7

       -oddp  same as -parenb cs8

       [-]parity
              same as [-]evenp

       pass8  same as -parenb -istrip cs8

       -pass8 same as parenb istrip cs7

       raw    same as -ignbrk -brkint -ignpar -parmrk  -inpck  -istrip  -inlcr
              -igncr  -icrnl  -ixon  -ixoff -icanon -opost -isig -iuclc -ixany
              -imaxbel -xcase min 1 time 0

       -raw   same as cooked

       sane   same as cread -ignbrk brkint -inlcr -igncr icrnl  icanon  iexten
              echo  echoe  echok  -echonl  -noflsh -ixoff -iutf8 -iuclc -ixany
              imaxbel -xcase -olcuc -ocrnl opost -ofill onlcr  -onocr  -onlret
              nl0  cr0  tab0  bs0 vt0 ff0 isig -tostop -ofdel -echoprt echoctl
              echoke -extproc -flusho, all special characters to their default
              values

       Handle the tty line connected to standard  input.   Without  arguments,
       prints  baud  rate, line discipline, and deviations from stty sane.  In
       settings, CHAR is taken literally, or coded as in  ^c,  0x37,  0177  or
       127; special values ^- or undef used to disable special characters.

AUTHOR
       Written by David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/stty>
       or available locally via: info '(coreutils) stty invocation'

GNU coreutils 9.4                 April 2024                           STTY(1)
本文 更新于: 2026-03-06 09:52:35 创建于: 2026-03-06 09:52:35