0

Anyone with a complex .bashrc file has probably encountered this. If you type anything while Bash is loading, the characters you typed are printed both to the left and to the right of the prompt once it has loaded.

For example...

Steps to reproduce

  1. Add sleep 5 to the bottom of your .bashrc file
  2. Close and reopen your terminal emulator
  3. After the terminal loads, but before Bash has finished loading, type asdf

Actual result

asdfuser@hostname$ asdf

Desired result

user@hostname$ asdf

Is there any way to clear the text to the left of the prompt, or prevent it from ever showing up to begin with, without doing a clear or a reset?

(For whatever it's worth, I'm asking because I would like to programatically create a tmux session which prints certain text to the prompt without executing it. This keeps happening because tmux prints the text before Bash has finished loading. But this happens even outside of tmux like in the Steps to reproduce above, so I'm not sure how relevant my use of tmux is to this question.)

John Karahalis
  • 3,369
  • 2
  • 15
  • 20
  • 3
    `bash` knows nothing about your keyboard; its standard input is just an open file descriptor provided by its parent. Your *terminal* (or terminal emulator) is responsible for converting keyboard input into a stream of bytes to feed to the shell, and for echoing any keyboard input back to the screen in *addition* to passing it to the shell's input. – chepner Apr 15 '19 at 00:28
  • likewise when i log in, i must wait for the prompt to return after the username else the password is split around the password prompt `aPassword:***` ... not a good look. – ocæon Apr 15 '19 at 01:11
  • @chepner That makes sense. It makes me wonder: if the shell can clear that text after the fact (`reset`) could it not also be told to hide it to begin with? – John Karahalis Apr 15 '19 at 03:16
  • Again, Bash is running when the text is written. It has started reading its configuration but hasn't finished doing so. – John Karahalis Apr 15 '19 at 03:30
  • 1
    `reset` doesn't do anything except write a byte (or byte sequence) that your terminal emulator interprets as an instruction to clear the screen (instead of printing the bytes literally. It has nothing to do with the shell. – chepner Apr 15 '19 at 12:43

2 Answers2

0

The problem is that tmux and most other terminals will create ptys with ECHO enabled so the characters will be echoed by the kernel before the shell has even started. In theory you could prevent tmux from doing this and then have bash turn it on, but it would mean code changes to both and would probably cause problems with other applications.

A simple option is to have tmux sleep for short period after creating the pane to let the shell start, something like:

tmux splitw \; run "sleep 0.5" \; send "abcdef"

(Even smaller sleeps might work, you can experiment.)

I don't know about bash, but ksh at least seems to set the tty up as it likes it, so you could possibly also use wait-for, something like:

tmux splitw "stty -echo; tmux wait -S foo; exec $SHELL" \; wait foo \; send "abcdef"

I'd go with the sleep myself though.

Nicholas Marriott
  • 3,148
  • 9
  • 12
0

Someone suggested clearing the text, you could do that I guess, I know bash has C-l (L):

tmux splitw \; send C-l "abcdef"

This won't work for the likes of ssh obviously, only for the shell.

Nicholas Marriott
  • 3,148
  • 9
  • 12