BASH: how to view command history in while loop?
up vote
5
down vote
favorite
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)?
linux bash shell-script shell ubuntu
New contributor
add a comment |
up vote
5
down vote
favorite
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)?
linux bash shell-script shell ubuntu
New contributor
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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)?
linux bash shell-script shell ubuntu
New contributor
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)?
linux bash shell-script shell ubuntu
linux bash shell-script shell ubuntu
New contributor
New contributor
New contributor
asked 20 hours ago
user321630
362
362
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
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:
rlwrap -a ./script.sh
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.
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 Theread
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.
– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
add a comment |
up vote
0
down vote
You can use history -s
to edit the history list, and read -e
to make it possible to view the history.
#!/bin/bash
while true; do
read -rep $'n '"$USER"'> ' userInput
history -s "$userInput"
echo "$userInput"
done
Note that there are various options about the command history. The behavior may be very different between a script and an interactive shell. For example, the command history is not loaded from or saved to a file automatically in a script, which may or may not be desirable in your situation. But you can fix it by adding more code if not.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
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:
rlwrap -a ./script.sh
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.
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 Theread
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.
– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
add a comment |
up vote
8
down vote
accepted
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:
rlwrap -a ./script.sh
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.
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 Theread
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.
– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
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:
rlwrap -a ./script.sh
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.
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:
rlwrap -a ./script.sh
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.
edited 19 hours ago
answered 19 hours ago
Kusalananda
116k15218351
116k15218351
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 Theread
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.
– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
add a comment |
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 Theread
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.
– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
Thanks for your answer. So this isn't possible purely with bash?
– user321630
19 hours ago
@user321630 The
read
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.– Kusalananda
19 hours ago
@user321630 The
read
builtin does not provide this facility. You would have to implement it by saving the inputted data to a file and then somehow search that appropriately when the user uses the arrow keys.– Kusalananda
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
@Kusalananda I was thinking the same but is there any way to pass up-key as an argument? I searched a lot but no help.
– Debian_yadav
19 hours ago
1
1
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
@Debian_yadav This is non-trivial as the up-arrow key sends a series of characters, and you would have to distinguish these from other input and to do that without requiring the user to press Enter. Here's the solution to the first part of that problem: bashscript to detect right arrow key being pressed
– Kusalananda
19 hours ago
add a comment |
up vote
0
down vote
You can use history -s
to edit the history list, and read -e
to make it possible to view the history.
#!/bin/bash
while true; do
read -rep $'n '"$USER"'> ' userInput
history -s "$userInput"
echo "$userInput"
done
Note that there are various options about the command history. The behavior may be very different between a script and an interactive shell. For example, the command history is not loaded from or saved to a file automatically in a script, which may or may not be desirable in your situation. But you can fix it by adding more code if not.
add a comment |
up vote
0
down vote
You can use history -s
to edit the history list, and read -e
to make it possible to view the history.
#!/bin/bash
while true; do
read -rep $'n '"$USER"'> ' userInput
history -s "$userInput"
echo "$userInput"
done
Note that there are various options about the command history. The behavior may be very different between a script and an interactive shell. For example, the command history is not loaded from or saved to a file automatically in a script, which may or may not be desirable in your situation. But you can fix it by adding more code if not.
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use history -s
to edit the history list, and read -e
to make it possible to view the history.
#!/bin/bash
while true; do
read -rep $'n '"$USER"'> ' userInput
history -s "$userInput"
echo "$userInput"
done
Note that there are various options about the command history. The behavior may be very different between a script and an interactive shell. For example, the command history is not loaded from or saved to a file automatically in a script, which may or may not be desirable in your situation. But you can fix it by adding more code if not.
You can use history -s
to edit the history list, and read -e
to make it possible to view the history.
#!/bin/bash
while true; do
read -rep $'n '"$USER"'> ' userInput
history -s "$userInput"
echo "$userInput"
done
Note that there are various options about the command history. The behavior may be very different between a script and an interactive shell. For example, the command history is not loaded from or saved to a file automatically in a script, which may or may not be desirable in your situation. But you can fix it by adding more code if not.
answered 8 hours ago
user23013
457311
457311
add a comment |
add a comment |
user321630 is a new contributor. Be nice, and check out our Code of Conduct.
user321630 is a new contributor. Be nice, and check out our Code of Conduct.
user321630 is a new contributor. Be nice, and check out our Code of Conduct.
user321630 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482602%2fbash-how-to-view-command-history-in-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown