History expansion in scripts [duplicate]












1
















This question already has an answer here:




  • History substitution fails when implemented in shell script

    1 answer




I have the following Bash script



case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
set -H
case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
pwd
last_command="!!"
echo $last_command


which prints



disabled
enabled
/home/user
!!


The first line of code checks to see if history expansion is enabled. The second enables history expansion. The third is the same as the first. Then its runs pwd and finally assigns what should be last command to last_command and prints this variable.



The history is not expanding. What is going on?










share|improve this question







New contributor




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











marked as duplicate by Jeff Schaller bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 hour ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    1
















    This question already has an answer here:




    • History substitution fails when implemented in shell script

      1 answer




    I have the following Bash script



    case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
    set -H
    case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
    pwd
    last_command="!!"
    echo $last_command


    which prints



    disabled
    enabled
    /home/user
    !!


    The first line of code checks to see if history expansion is enabled. The second enables history expansion. The third is the same as the first. Then its runs pwd and finally assigns what should be last command to last_command and prints this variable.



    The history is not expanding. What is going on?










    share|improve this question







    New contributor




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











    marked as duplicate by Jeff Schaller bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    1 hour ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • History substitution fails when implemented in shell script

        1 answer




      I have the following Bash script



      case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
      set -H
      case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
      pwd
      last_command="!!"
      echo $last_command


      which prints



      disabled
      enabled
      /home/user
      !!


      The first line of code checks to see if history expansion is enabled. The second enables history expansion. The third is the same as the first. Then its runs pwd and finally assigns what should be last command to last_command and prints this variable.



      The history is not expanding. What is going on?










      share|improve this question







      New contributor




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













      This question already has an answer here:




      • History substitution fails when implemented in shell script

        1 answer




      I have the following Bash script



      case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
      set -H
      case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac
      pwd
      last_command="!!"
      echo $last_command


      which prints



      disabled
      enabled
      /home/user
      !!


      The first line of code checks to see if history expansion is enabled. The second enables history expansion. The third is the same as the first. Then its runs pwd and finally assigns what should be last command to last_command and prints this variable.



      The history is not expanding. What is going on?





      This question already has an answer here:




      • History substitution fails when implemented in shell script

        1 answer








      bash shell-script shell command-history history-expansion






      share|improve this question







      New contributor




      History_expansion 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




      History_expansion 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




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









      asked 1 hour ago









      History_expansionHistory_expansion

      82




      82




      New contributor




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





      New contributor





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






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




      marked as duplicate by Jeff Schaller bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      1 hour ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Jeff Schaller bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      1 hour ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          For a non-interactive shell, you must specifically also enable command history:



          set -o history
          set -o histexpand


          Then your example will work:



          disabled
          enabled
          /home/schaller/tmp/502442
          last_command="pwd"
          pwd





          share|improve this answer
























          • Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

            – History_expansion
            1 hour ago


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          For a non-interactive shell, you must specifically also enable command history:



          set -o history
          set -o histexpand


          Then your example will work:



          disabled
          enabled
          /home/schaller/tmp/502442
          last_command="pwd"
          pwd





          share|improve this answer
























          • Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

            – History_expansion
            1 hour ago
















          0














          For a non-interactive shell, you must specifically also enable command history:



          set -o history
          set -o histexpand


          Then your example will work:



          disabled
          enabled
          /home/schaller/tmp/502442
          last_command="pwd"
          pwd





          share|improve this answer
























          • Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

            – History_expansion
            1 hour ago














          0












          0








          0







          For a non-interactive shell, you must specifically also enable command history:



          set -o history
          set -o histexpand


          Then your example will work:



          disabled
          enabled
          /home/schaller/tmp/502442
          last_command="pwd"
          pwd





          share|improve this answer













          For a non-interactive shell, you must specifically also enable command history:



          set -o history
          set -o histexpand


          Then your example will work:



          disabled
          enabled
          /home/schaller/tmp/502442
          last_command="pwd"
          pwd






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Jeff SchallerJeff Schaller

          42k1156133




          42k1156133













          • Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

            – History_expansion
            1 hour ago



















          • Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

            – History_expansion
            1 hour ago

















          Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

          – History_expansion
          1 hour ago





          Thanks, how do I make is so that I can access the history from the shell that is running the script and use history expansion is a similar fashion?

          – History_expansion
          1 hour ago



          Popular posts from this blog

          濃尾地震

          How to rewrite equation of hyperbola in standard form

          No ethernet ip address in my vocore2