【问题标题】:Why does ‘stty -echo’ not turn off the echo bit on macOS为什么“stty -echo”没有关闭 macOS 上的回显位
【发布时间】:2021-10-22 14:54:19
【问题描述】:

我正在阅读 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 上运行呢?

【问题讨论】:

    标签: macos unix stty


    【解决方案1】:

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

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

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

    更新

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

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

    stty -a; stty -echo; stty -a
    

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

    unsetopt ZLE
    

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

    【讨论】:

    • 嗨菲利普,感谢您的出色回答。我刚刚在我的 Mac 上将 shell 从 zsh 切换到 bash,并且 stty-echo 成功关闭了 echo 位!所以我目前的问题是为什么不同的 shell 会影响 stty 的行为,为什么 zsh 不能这样做?再次感谢! :)
    • 嗨菲利普,我刚刚检查了这个问题 (unix.stackexchange.com/questions/343088/…) 以及第一个答案中邮件讨论的链接。老实说,我仍然不清楚这个问题:(,我想知道是否有让 zsh 的行为与 bash 相同。再次感谢 Philippe :)
    猜你喜欢
    • 2014-05-15
    • 2020-08-29
    • 2021-02-26
    • 2016-06-15
    • 2014-09-15
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多