I'm trying to send an arrow key via the stdin to the bash:
cat | /bin/bash
then i am typing "echo hi" => "hi" appears on the console (of course without the quotes) then i press the arrow key up => ^[[A command not found appears
Is it possible to send an arrow key to an program via the stdin ?
The reason why i am asking is: I want to control the bash from another programm. I would like to send arrow keys to the bash
What you really should be doing is create a pseudo-tty device (using openpty()
or similar), start bash
on that PTY, and send your key strokes through that PTY device. See the section on “Pseudo-Terminals” in the GNU C Library Manual.
Don't use cat
. Use the Bash builtin read
command with the -e
option to enable readline support.
# version 1
while IFS="" read -r -e -d $'\n' line; do printf '%s\n' "$line"; done | /bin/bash
# version 2
#set -o pipefail
# kill 0: kill process group
(
while IFS="" read -r -e -d $'\n' line; do
#trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR
trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR
printf '%s\n' "$line" >> ~/.bash_history
history -n
printf '%s\n' "$line"
done
) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill 0' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)
#) | (trap 'trap - EXIT HUP INT QUIT PIPE TERM ERR; kill $PPID' EXIT HUP INT QUIT PIPE TERM ERR; /bin/bash)
尝试使用-i
开关启动bash。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.