我正在阅读Bruce Molay的《Understanding Unix/Linux Programming》。在第五章中,本书介绍了一个新的命令stty
,它可以帮助您配置终端设置。
书中提到的一个例子是使用stty -echo
来关闭回声,然后我在我的Mac上尝试了它,但发现它根本不起作用。
// here is some infomation about my mac
Mac mini (M1, 2020)
OS version: 11.4
// by the way, I also tested it on my old intel mac, and I got the same result
Macbook Pro(Early 2015)
OS version: 10.15.6
// here are the commands I used in the terminal on Mac
➜ ~ stty -a # check the old 'echo bit' status
speed 38400 baud; 45 rows; 139 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
min = 1; quit = ^; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
➜ ~ stty -echo # turn off echo bit
➜ ~ stty -a # see the results
speed 38400 baud; 45 rows; 139 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
min = 1; quit = ^; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
然后我在ubuntu中测试它docker容器,stty -echo
工作良好.
那么为什么它不能在macOS上工作呢?
交互式shell保留了一些对stty设置的控制。
在Ubuntu中,你使用的是bash,而在MacOS中,你使用的是zsh。
作为测试,在MacOS中运行/bin/bash并进行测试。
交互shell不影响stty的行为,它们可以改变tty的属性,相当于stty echo
,就在显示提示符之前。
可以通过运行zsh:
来验证。stty -a; stty -echo; stty -a
要求zsh不执行stty echo
,可以这样做:
unsetopt ZLE
但是这会禁用zsh的编辑功能,这不是你想要的。