History expansion in scripts [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2
















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













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

);
);
);
Feb 23 at 2:47


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.


















  • Closely related: unix.stackexchange.com/q/384861/117549

    – Jeff Schaller
    Feb 23 at 14:35















2
















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













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

);
);
);
Feb 23 at 2:47


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.


















  • Closely related: unix.stackexchange.com/q/384861/117549

    – Jeff Schaller
    Feb 23 at 14:35













2












2








2


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















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













share|improve this question











share|improve this question




share|improve this question










asked Feb 23 at 2:31









History_expansionHistory_expansion

154




154




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

);
);
);
Feb 23 at 2:47


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

);
);
);
Feb 23 at 2:47


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.














  • Closely related: unix.stackexchange.com/q/384861/117549

    – Jeff Schaller
    Feb 23 at 14:35

















  • Closely related: unix.stackexchange.com/q/384861/117549

    – Jeff Schaller
    Feb 23 at 14:35
















Closely related: unix.stackexchange.com/q/384861/117549

– Jeff Schaller
Feb 23 at 14:35





Closely related: unix.stackexchange.com/q/384861/117549

– Jeff Schaller
Feb 23 at 14:35










1 Answer
1






active

oldest

votes


















1














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
    Feb 23 at 2:53











  • @History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

    – Sergiy Kolodyazhnyy
    Feb 23 at 4:26











  • @SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

    – History_expansion
    Feb 23 at 11:48











  • @History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

    – Jeff Schaller
    Feb 23 at 13:49











  • @JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

    – History_expansion
    Feb 23 at 14:41

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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
    Feb 23 at 2:53











  • @History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

    – Sergiy Kolodyazhnyy
    Feb 23 at 4:26











  • @SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

    – History_expansion
    Feb 23 at 11:48











  • @History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

    – Jeff Schaller
    Feb 23 at 13:49











  • @JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

    – History_expansion
    Feb 23 at 14:41















1














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
    Feb 23 at 2:53











  • @History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

    – Sergiy Kolodyazhnyy
    Feb 23 at 4:26











  • @SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

    – History_expansion
    Feb 23 at 11:48











  • @History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

    – Jeff Schaller
    Feb 23 at 13:49











  • @JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

    – History_expansion
    Feb 23 at 14:41













1












1








1







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 Feb 23 at 2:45









Jeff SchallerJeff Schaller

43.8k1161141




43.8k1161141












  • 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
    Feb 23 at 2:53











  • @History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

    – Sergiy Kolodyazhnyy
    Feb 23 at 4:26











  • @SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

    – History_expansion
    Feb 23 at 11:48











  • @History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

    – Jeff Schaller
    Feb 23 at 13:49











  • @JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

    – History_expansion
    Feb 23 at 14:41

















  • 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
    Feb 23 at 2:53











  • @History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

    – Sergiy Kolodyazhnyy
    Feb 23 at 4:26











  • @SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

    – History_expansion
    Feb 23 at 11:48











  • @History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

    – Jeff Schaller
    Feb 23 at 13:49











  • @JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

    – History_expansion
    Feb 23 at 14:41
















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
Feb 23 at 2:53





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
Feb 23 at 2:53













@History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

– Sergiy Kolodyazhnyy
Feb 23 at 4:26





@History_expansion According to this history expansion is disabled for scripts by default (which is also mentioned under appropriate section in bash manual) and you should be able to enable that via set -H

– Sergiy Kolodyazhnyy
Feb 23 at 4:26













@SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

– History_expansion
Feb 23 at 11:48





@SergiyKolodyazhnyy I did that and showed it my question, it doesn't work.

– History_expansion
Feb 23 at 11:48













@History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

– Jeff Schaller
Feb 23 at 13:49





@History_expansion let's make a new question out of it. Describe exactly what you want to happen. I'm curious why you need the behavior, and if sourcing the script could be an option. Thanks!

– Jeff Schaller
Feb 23 at 13:49













@JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

– History_expansion
Feb 23 at 14:41





@JeffSchaller unix.stackexchange.com/questions/502526/… Thanks. I need this behavior because there's a blackbox in the company that reads a script (the script that I'm trying to write). I don't really need history expansion, I can do it in other ways, I'm very curious in how to get history expansion to work to this effect.

– History_expansion
Feb 23 at 14:41


Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)