BASH: how to view command history in while loop?











up vote
5
down vote

favorite
2












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)?










share|improve this question


























    up vote
    5
    down vote

    favorite
    2












    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)?










    share|improve this question
























      up vote
      5
      down vote

      favorite
      2









      up vote
      5
      down vote

      favorite
      2






      2





      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)?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 20:49









      user321630

      362




      362






















          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.






          share|improve this answer























          • 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










          • @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




















          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.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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.






            share|improve this answer























            • 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










            • @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

















            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.






            share|improve this answer























            • 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










            • @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















            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.






            share|improve this 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:



            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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






            • 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










            • @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






            • 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














            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 8:15









                user23013

                477311




                477311






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents