I have a simple while loop accepting input:
#!/bin/bash
while true; do
read -rep $'\n '"$USER"'> ' userInput
echo "$userInput"
done
Example:
./input.sh
username> command1
command1
username> command2
command2
Is it possible to have a command history? So that I can press up on my keyboard to view the previously executed commands (without leaving the while loop)?
Best Answer
You could use the small Readline wrapper
rlwrap
. This is a neat little tool that provides command history to utilities that don't implement it by themselves.You would use
rlwrap
on the script itself:This would save a history file called
~/.script.sh_history
and would use that file not only in the current session, but also in future sessions to provide a sort of history that you could step through.See the manual for
rlwrap
.rlwrap
is commonly available as a package on most Unices, but may also be had from its GitHub repository.