1

Open xterm, run tty and see pseudo terminal slave file (let's say it is /dev/pts/0). Then open another xterm and run

$ stty -F /dev/pts/0
speed 38400 baud; line = 0;
lnext = <undef>; discard = <undef>; min = 1; time = 0;
-brkint -icrnl -imaxbel iutf8
-icanon -echo

Then run /bin/sleep 1000 in first xterm. Then run the same stty command in second xterm again:

$ stty -F /dev/pts/0
speed 38400 baud; line = 0;
-brkint -imaxbel iutf8

Then terminate sleep command in first xterm. Then run the same stty command in second xterm again:

$ stty -F /dev/pts/0
speed 38400 baud; line = 0;
lnext = <undef>; discard = <undef>; min = 1; time = 0;
-brkint -icrnl -imaxbel iutf8
-icanon -echo

We see that bash changes tty attributes before running a command and restores them back after running a command. Where is it described in bash documentation? Are all tty attributes restored, or some attributes may not be restored if they are changed by program?

  • have not yet fully grasped what you demonstrate. Just a first remark: using stty on a PTY terminal is probably a complicated experiment. X windows will be controlling some of the TTY settings. "tty attributes" are likely to be spread out a bit. stty has limited effect and precision here. – user373503 Oct 18 '19 at 9:20
  • "We see that bash changes tty attributes..." I turn it around: by putting bash to sleep, the status of that terminal is changed and maybe even adapted by xterm -- put to "standby". – user373503 Oct 18 '19 at 9:24
2

That's the readline(3) line-editing library, which is usually statically built as part of bash, but is also used by other programs.

Every time it starts reading a command from the user, readline saves the terminal settings, and puts the terminal into "raw" mode [1], so it could be able to handle moving the insertion point right and left, recall commands from the history etc. When readline(3) returns (eg. when the user has pressed Enter), the original settings of the terminal are restored. Readline will also mess with signals, which may result in some puzzling behaviour.

If you strace bash, look for ioctl(TCSETS*) (which implements tcsetattr(3)) and for ioctl(TCGETS) (tcgetattr(3)). Those are the same functions used by stty(1). If you run bash with --noediting you will see that it leaves the terminal settings alone.

[1] not exactly the "raw" mode of cfmakeraw(3); you can see the exact details here. All those terminal settings are documented in the termios(3) manpage.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.