2

When my program is reading user input, the vertical arrow keys do nothing. If the line discipline was echoing everything to the master side, wouldn't the cursor move up to the previous line of the terminal? If I'm correct, are there other characters that the line discipline doesn't echo?

As a side note, I thought bash used "raw mode" (which disables echo among other things), but running stty -echo "hangs" the terminal, which suggests that echo is enabled during normal bash execution.

Zsh doesn't exhibit this behaviour, but I think that's because it resets the echo flag when a command exits.

2
  • Note: in bash if you execute stty -echo, the terminal will just not print back what you are writting. if you write reset and enter it will just execute the reset commands and restore the eco setting. ZSH: it just set echo again (the | means set, which may be the reset you are writing, but let's be explicit: it always set echo) Commented Oct 6, 2022 at 15:10
  • What is that "my program"?
    – egmont
    Commented Oct 7, 2022 at 6:37

2 Answers 2

1

Hard to answer without knowing what "my program" refers to, and how that program perhaps alters the terminal line's settings.

It could be a text editor, which then lets you move the cursor in the file. It could be another shell, which then probably scrolls through the history entries. It could be an audio control software, increasing/decreasing the volume. Etc.

If it's something like sleep 1000, you'll see ^[[A and ^[[B appearing because the line discipline doesn't echo back them in their raw form, but rather converting the ESC byte into the human-readable literal ^ and [ characters.

If it's cat, when you hit Enter then you'll see the cursor moving vertically. The reason for the delay is that line disciple delays sending the characters to the application (i.e. cat) until the entire input line is completed. The reason for the cursor moving vertically has nothing to do with the line discipline though, it's simply because cat echoes back the raw data, and because the input escape sequences generated by the arrow keys are the same as the output escape sequences moving the cursor in the said direction.

1

TUI applications or more generally applications that handle keyboard input by themselves have to disable both the line discipline own line editor (ICANON) and local echo (ECHO) as they need to read the characters sent upon key presses as them come and do their own echo.

Those applications, upon startup change the termios settings and upon leaving (or suspension) restore them.

readline (the line editor used by bash) and zle (the zsh line editor) are no exception.

Those line editors are started when the shell issues its prompt and stopped when you execute a command.

So, when you run stty -a or any other command, you don't see the termios setting as used within readline / zle, you see the ones as restored by them when they left.

When ECHO is on, input from the terminal is echoed back, but for control characters there are two other things to take into account, and a third one for arrow keys specifically:

  • if ICANON is on (as it is by default), the line discipline internal line editor will intercept some control characters such as BS or DEL, ^W, ^U for their own line editing. ESC (the first character sent upon arrow keys is generally not of those though).
  • if ECHOCTL is also on (which it also generally is by default), control chacters are echoed as ^X instead of as-is.
  • terminals often can send different escape sequences whether they're in "keypad transmit" mode or not. You'll notice that after tput smkx, the Up key sends \eOA and after tput rmkx, it sends the \e[A sequence, which happens to correspond to the sequence to move the cursor up (same as sent by tput cuu1). There are other modes supported by some terminals that affect what is send upon key presses which we won't discuss here.

So, for the terminal discipline local echo to move the cursor around when you press the arrow keys, you'd need:

tput rmkx; stty -icanon echo -echoctl; cat > /dev/null

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .