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
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
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
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
asked Nov 18 at 20:49
user321630
362
362
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
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
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
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
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
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
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 Nov 18 at 21:31
answered Nov 18 at 21:23
Kusalananda
119k16223365
119k16223365
Thanks for your answer. So this isn't possible purely with bash?
– user321630
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
add a comment |
Thanks for your answer. So this isn't possible purely with bash?
– user321630
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
Thanks for your answer. So this isn't possible purely with bash?
– user321630
Nov 18 at 21:31
Thanks for your answer. So this isn't possible purely with bash?
– user321630
Nov 18 at 21:31
@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
Nov 18 at 21:34
@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
Nov 18 at 21:34
@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
Nov 18 at 21:36
@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
Nov 18 at 21:36
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
Nov 18 at 21:40
@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
Nov 18 at 21:40
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 Nov 19 at 8:15
user23013
477311
477311
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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