0

EDIT: As per @JdeBP's comment, I've learned that the history command and the history recall functionality of interactive terminals are two separate things. I am, however, still curious about how the GNU readline history recall functionality prints to the screen.

I've come to realize that the bash history command does not use stdout to print previous commands to the terminal window. I came across this while exploring a separate issue (link), and now I am curious how exactly it works.

When you type a command and press enter, you can use the up arrow to pull the previous command up. However, the previous command isn't sent through stdout. This can be seen by redirecting all of stdout to a coprocess which modifies the output before printing it to the screen. The below script does exactly this. It simply echos what you type, but also redirects stdout to a sed command beforehand.

exec 4>&1
coproc out (sed 's/^/PREFIX /' >&4)
exec 1>&${out[1]}

while read -e x; do
   echo $x
done

When executing this script, anything you type will be printed with "PREFIX" prefixed. However, when you use the up arrow to access previous commands, there is no "PREFIX" prepended.

Why is this? How does GNU history recall place the text from the history file onto the terminal screen? If it doesn't use stdout, does it utilize stderr, or something else? I've tried redirecting stderr in the same manner, but this causes my text to not echo to the terminal screen, making the up/down arrows print nothing. Thus I cannot verify. I suppose this is probably indication that this history functionality is using stderr, but I'd just like to confirm.

  • Relating https://unix.stackexchange.com/questions/380012/why-does-bash-interactive-shell-by-default-write-its-prompt-and-echoes-its-inter – Jeff Schaller Dec 04 '19 at 16:13
  • You are erroneously conflating the history command with history recall in GNU Readline. And another related question is https://unix.stackexchange.com/a/434839/5132 . – JdeBP Dec 04 '19 at 16:15
  • @JdeBP Ah, good to know. So does the GNU readline history recall functionality use stderr? I had originally thought this, but redirecting stderr in the same manner as I did with stdout in my question has the unfortunate side effect that I am not able to see the output of up/down arrows in my terminal at all, and so I am unable to confirm this with my method. – ExecutionByFork Dec 04 '19 at 18:38
  • Please fix your question so that it talks about the right thing, which is not the history command. And put what you've just asked in the question, too. – JdeBP Dec 04 '19 at 19:02

0 Answers0