0

我正在阅读 Bruce Molay 的《Understanding Unix/Linux Programming》。在第 5 章中,本书介绍了一个新命令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 上运行呢?

4

1 回答 1

0

交互式 shell 保留了对 stty 设置的一些控制。

在 Ubuntu 中,您使用的是 bash,而在 MacOS 中,您使用的是 zsh。

作为测试,在 MacOS 中运行 /bin/bash 并进行测试。

更新

Interactives shell 不会影响 stty 的行为,它们可以stty echo在显示提示之前更改 tty 的等价属性。

您可以通过在 zsh 中运行来验证这一点:

stty -a; stty -echo; stty -a

要让 zsh 不要这样做stty echo,你可以这样做:

unsetopt ZLE

但这会禁用 zsh 的编辑功能,这不是您想要的。

于 2021-08-21T12:08:05.663 回答