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







New contributor




user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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







    New contributor




    user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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







      New contributor




      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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







      New contributor




      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 20 hours ago









      user321630

      362




      362




      New contributor




      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user321630 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















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






          • 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




















          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
            });


            }
            });






            user321630 is a new contributor. Be nice, and check out our Code of Conduct.










             

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






            • 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

















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






            • 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















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






            • 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










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






            • 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














            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 8 hours ago









                user23013

                457311




                457311






















                    user321630 is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    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.















                     


                    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

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules