bash works with STDIN, STDOUT and STDERR. When xterm is opened, it allocates the pseudo tty. Then xterm forks child process and STDIN, STDOUT, STDERR are tied to slave_fd via dup2 and exec is called with bash. This is good.

But how to make bash work with TTY directly? How to make bash execute open("/dev/ttyS0", O_RDWR)? So that I could attach a device to /dev/ttyS0 and execute commands in bash from the device.

  • 1
    Take a look into the agetty utility. It handles the TTY stuff, then does a login or starts an alternative program. – Janka Aug 21 '19 at 1:55
  • @Janka I see - TTY is opened by a separate program. So, NO program is supposed to open a TTY directly? Only a dedicated program? The other way around: what if I have a program which does interaction via TTY by doing open("/dev/ttyS0", O_RDWR) and sometimes I want to interact with the program via xterm, without changing the program itself - is this possible? – Igor Liferenko Aug 21 '19 at 2:21
  • 2
    exec 7<>/dev/ttyS0; stty <&7 [your stty settings]; cmd1 >&7; cmd2 <&7; cmd3 <&7 >&7 2>&1. Setting the right modes & speed and using serial devices from the shell and parsing responses is tricky, though. – Uncle Billy Aug 21 '19 at 4:07
  • That comment would merit being turned into an answer, @UncleBilly. – roaima Aug 21 '19 at 6:55
  • What kind of a device is it? Is it a terminal and you want to log in from it (have a shell listening to commands from the serial line); or is it something like a switch that provides its own console on the serial line and you want to connect to it? – ilkkachu Aug 21 '19 at 6:55

If you know the baud rate you can use screen to connect serial terminals like : screen /dev/ttyS0 115200

  • Can screen be used to connect to pseudo terminals? This does not work: screen /dev/pts/X (where /dev/pts/X was created by opening a new xterm window and issuing kill -STOP $$ in it to prevent bash from messing with the tty but at the same time keep terminal window open to use the tty). – Igor Liferenko Aug 22 '19 at 0:52

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.