2

When I try bind some commands with bind -x bash utility, I can't see my stdin input on terminal.

Example:

root> # bind -x '"\C-p": su dargod'

When I type ctrl+P - I login to user dargod, but all what I types not shows. Same situation with bind ssh command for connect.

How I can normally interract with shell?

Info:

Bash 3.1.17 (CentOS 5)
Bash 4.2.46 (CentOS 7)
Bash 4.1.2 (CentOS 6)
Bash 5.0.7 (Arch Linux)
Bash 4.2.45 (Gentoo)

Dargod
  • 55
  • What is the info? Are those all the machines you have tried on or does your machine shift between operating systems and bash versions? – jesse_b Aug 02 '19 at 20:45
  • Yes, In the info indicated on which systems I tried and with which versions bash – Dargod Aug 02 '19 at 20:48

1 Answers1

4

Commands bound to a key are meant to be used as part of line editing. They run with the terminal in raw mode without echo, with bash expecting to handle each key press¹. Most commands expect to run with the terminal in cooked mode, where the terminal reads one line at a time and echoes the input.

To run a command that takes over the terminal interaction, temporarily set the terminal to cooked mode.

bind -x '"\C-p": _bash_stty_save=$(stty -g); stty sane; su dargod; stty "$_bash_stty_save"'

Note that this may still not work perfectly, because you're doing something that bash doesn't expect. Bindings are not the right tool to run a command. To define a shortcut for a command, use an alias.


¹ strictly speaking, it's not a complete raw mode as ^C, ^Z,^\ (isig) and ^Q / ^S (ixon) are not disabled in bash' line editor (even though ^S conflicts with its emacs mode incremental search).