How to get last N commands from history?












28















I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.



$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2


So my question is: do I have to parse the output of the command to accomplish this?










share|improve this question




















  • 1





    // , This is why I love stackexchange

    – Nathan Basanese
    Mar 7 '18 at 20:53
















28















I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.



$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2


So my question is: do I have to parse the output of the command to accomplish this?










share|improve this question




















  • 1





    // , This is why I love stackexchange

    – Nathan Basanese
    Mar 7 '18 at 20:53














28












28








28


9






I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.



$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2


So my question is: do I have to parse the output of the command to accomplish this?










share|improve this question
















I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.



$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2


So my question is: do I have to parse the output of the command to accomplish this?







bash command-line command-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 '15 at 22:37









Gilles

534k12810791596




534k12810791596










asked Jun 29 '15 at 14:28









fedorquifedorqui

4,11222056




4,11222056








  • 1





    // , This is why I love stackexchange

    – Nathan Basanese
    Mar 7 '18 at 20:53














  • 1





    // , This is why I love stackexchange

    – Nathan Basanese
    Mar 7 '18 at 20:53








1




1





// , This is why I love stackexchange

– Nathan Basanese
Mar 7 '18 at 20:53





// , This is why I love stackexchange

– Nathan Basanese
Mar 7 '18 at 20:53










3 Answers
3






active

oldest

votes


















31














I found it!




history [n]



An argument of n lists only the last n lines.




$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2





share|improve this answer



















  • 3





    That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

    – Stéphane Chazelas
    Jun 29 '15 at 14:59






  • 1





    I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

    – fedorqui
    Jun 29 '15 at 16:01








  • 5





    fc -l -2 works too (but doesn't add current fc -l -2 to output)

    – Evgeny Vereshchagin
    Jun 30 '15 at 3:44











  • See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

    – Evgeny Vereshchagin
    Jun 30 '15 at 4:57











  • fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

    – Timo
    Mar 13 '18 at 20:44





















0














Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).



history [b]


Shows all the history beginning from an entry with a number [b]



history [b] [e]


Shows the history interval from [b] to [e]






share|improve this answer































    0














    When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:



    alias hs='history 16 | head -n 15'
    (the command itself history 16 | head -n 15)



    Another useful history alias:
    alias hsg='history | grep '
    (when ctr+R is too small to choose from)





    share








    New contributor




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




















      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',
      autoActivateHeartbeat: false,
      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%2f212872%2fhow-to-get-last-n-commands-from-history%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      31














      I found it!




      history [n]



      An argument of n lists only the last n lines.




      $ echo "hello
      how are you"
      $ history 2
      1060 echo "hello
      how are you"
      1061 history 2





      share|improve this answer



















      • 3





        That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

        – Stéphane Chazelas
        Jun 29 '15 at 14:59






      • 1





        I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

        – fedorqui
        Jun 29 '15 at 16:01








      • 5





        fc -l -2 works too (but doesn't add current fc -l -2 to output)

        – Evgeny Vereshchagin
        Jun 30 '15 at 3:44











      • See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

        – Evgeny Vereshchagin
        Jun 30 '15 at 4:57











      • fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

        – Timo
        Mar 13 '18 at 20:44


















      31














      I found it!




      history [n]



      An argument of n lists only the last n lines.




      $ echo "hello
      how are you"
      $ history 2
      1060 echo "hello
      how are you"
      1061 history 2





      share|improve this answer



















      • 3





        That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

        – Stéphane Chazelas
        Jun 29 '15 at 14:59






      • 1





        I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

        – fedorqui
        Jun 29 '15 at 16:01








      • 5





        fc -l -2 works too (but doesn't add current fc -l -2 to output)

        – Evgeny Vereshchagin
        Jun 30 '15 at 3:44











      • See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

        – Evgeny Vereshchagin
        Jun 30 '15 at 4:57











      • fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

        – Timo
        Mar 13 '18 at 20:44
















      31












      31








      31







      I found it!




      history [n]



      An argument of n lists only the last n lines.




      $ echo "hello
      how are you"
      $ history 2
      1060 echo "hello
      how are you"
      1061 history 2





      share|improve this answer













      I found it!




      history [n]



      An argument of n lists only the last n lines.




      $ echo "hello
      how are you"
      $ history 2
      1060 echo "hello
      how are you"
      1061 history 2






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jun 29 '15 at 14:28









      fedorquifedorqui

      4,11222056




      4,11222056








      • 3





        That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

        – Stéphane Chazelas
        Jun 29 '15 at 14:59






      • 1





        I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

        – fedorqui
        Jun 29 '15 at 16:01








      • 5





        fc -l -2 works too (but doesn't add current fc -l -2 to output)

        – Evgeny Vereshchagin
        Jun 30 '15 at 3:44











      • See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

        – Evgeny Vereshchagin
        Jun 30 '15 at 4:57











      • fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

        – Timo
        Mar 13 '18 at 20:44
















      • 3





        That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

        – Stéphane Chazelas
        Jun 29 '15 at 14:59






      • 1





        I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

        – fedorqui
        Jun 29 '15 at 16:01








      • 5





        fc -l -2 works too (but doesn't add current fc -l -2 to output)

        – Evgeny Vereshchagin
        Jun 30 '15 at 3:44











      • See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

        – Evgeny Vereshchagin
        Jun 30 '15 at 4:57











      • fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

        – Timo
        Mar 13 '18 at 20:44










      3




      3





      That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

      – Stéphane Chazelas
      Jun 29 '15 at 14:59





      That seems to be for tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.

      – Stéphane Chazelas
      Jun 29 '15 at 14:59




      1




      1





      I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

      – fedorqui
      Jun 29 '15 at 16:01







      I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799

      – fedorqui
      Jun 29 '15 at 16:01






      5




      5





      fc -l -2 works too (but doesn't add current fc -l -2 to output)

      – Evgeny Vereshchagin
      Jun 30 '15 at 3:44





      fc -l -2 works too (but doesn't add current fc -l -2 to output)

      – Evgeny Vereshchagin
      Jun 30 '15 at 3:44













      See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

      – Evgeny Vereshchagin
      Jun 30 '15 at 4:57





      See also: How to stop bash replacing commands in history with asterisks ? (‘*’).

      – Evgeny Vereshchagin
      Jun 30 '15 at 4:57













      fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

      – Timo
      Mar 13 '18 at 20:44







      fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.

      – Timo
      Mar 13 '18 at 20:44















      0














      Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).



      history [b]


      Shows all the history beginning from an entry with a number [b]



      history [b] [e]


      Shows the history interval from [b] to [e]






      share|improve this answer




























        0














        Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).



        history [b]


        Shows all the history beginning from an entry with a number [b]



        history [b] [e]


        Shows the history interval from [b] to [e]






        share|improve this answer


























          0












          0








          0







          Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).



          history [b]


          Shows all the history beginning from an entry with a number [b]



          history [b] [e]


          Shows the history interval from [b] to [e]






          share|improve this answer













          Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).



          history [b]


          Shows all the history beginning from an entry with a number [b]



          history [b] [e]


          Shows the history interval from [b] to [e]







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 12 '18 at 15:41









          CrimsonPermanentAssurerCrimsonPermanentAssurer

          112




          112























              0














              When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:



              alias hs='history 16 | head -n 15'
              (the command itself history 16 | head -n 15)



              Another useful history alias:
              alias hsg='history | grep '
              (when ctr+R is too small to choose from)





              share








              New contributor




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

























                0














                When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:



                alias hs='history 16 | head -n 15'
                (the command itself history 16 | head -n 15)



                Another useful history alias:
                alias hsg='history | grep '
                (when ctr+R is too small to choose from)





                share








                New contributor




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























                  0












                  0








                  0







                  When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:



                  alias hs='history 16 | head -n 15'
                  (the command itself history 16 | head -n 15)



                  Another useful history alias:
                  alias hsg='history | grep '
                  (when ctr+R is too small to choose from)





                  share








                  New contributor




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










                  When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:



                  alias hs='history 16 | head -n 15'
                  (the command itself history 16 | head -n 15)



                  Another useful history alias:
                  alias hsg='history | grep '
                  (when ctr+R is too small to choose from)






                  share








                  New contributor




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








                  share


                  share






                  New contributor




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









                  answered 6 mins ago









                  Alexey K.Alexey K.

                  1




                  1




                  New contributor




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





                  New contributor





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






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






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f212872%2fhow-to-get-last-n-commands-from-history%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

                      Histoire des bourses de valeurs

                      Why is there Russian traffic in my log files?

                      Rename multiple files to decrement number in file name?