History inside a Bash Loop

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











up vote
0
down vote

favorite












I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :



set -o history -o histexpand


But if I execute the following script:



#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history


I can see that 'history' does not track commands inside the for loop :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history


Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history









share|improve this question



















  • 2




    May I know your concern for doing this. do you want to track your script flow while executing?
    – msp9011
    Sep 5 at 11:05











  • Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
    – Richard
    Sep 5 at 12:38










  • Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
    – Richard
    Sep 5 at 13:16















up vote
0
down vote

favorite












I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :



set -o history -o histexpand


But if I execute the following script:



#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history


I can see that 'history' does not track commands inside the for loop :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history


Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history









share|improve this question



















  • 2




    May I know your concern for doing this. do you want to track your script flow while executing?
    – msp9011
    Sep 5 at 11:05











  • Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
    – Richard
    Sep 5 at 12:38










  • Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
    – Richard
    Sep 5 at 13:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :



set -o history -o histexpand


But if I execute the following script:



#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history


I can see that 'history' does not track commands inside the for loop :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history


Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history









share|improve this question















I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :



set -o history -o histexpand


But if I execute the following script:



#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history


I can see that 'history' does not track commands inside the for loop :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history


Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :



[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history






bash command-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 17:56









Rui F Ribeiro

36.8k1273117




36.8k1273117










asked Sep 5 at 10:50









Richard

11




11







  • 2




    May I know your concern for doing this. do you want to track your script flow while executing?
    – msp9011
    Sep 5 at 11:05











  • Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
    – Richard
    Sep 5 at 12:38










  • Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
    – Richard
    Sep 5 at 13:16













  • 2




    May I know your concern for doing this. do you want to track your script flow while executing?
    – msp9011
    Sep 5 at 11:05











  • Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
    – Richard
    Sep 5 at 12:38










  • Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
    – Richard
    Sep 5 at 13:16








2




2




May I know your concern for doing this. do you want to track your script flow while executing?
– msp9011
Sep 5 at 11:05





May I know your concern for doing this. do you want to track your script flow while executing?
– msp9011
Sep 5 at 11:05













Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
– Richard
Sep 5 at 12:38




Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
– Richard
Sep 5 at 12:38












Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
– Richard
Sep 5 at 13:16





Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
– Richard
Sep 5 at 13:16











1 Answer
1






active

oldest

votes

















up vote
1
down vote













The "history" is a history of "previously typed" commands.



From the GNU manual:



"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."



So, it does neither substitute shell variables, nor does it save loop iterations.



For this you could use bash debugging by calling your script so:



bash -x myscript > to-my-log-file


Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).






share|improve this answer




















  • ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
    – Richard
    Sep 5 at 17:45










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: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
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%2f466987%2fhistory-inside-a-bash-loop%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













The "history" is a history of "previously typed" commands.



From the GNU manual:



"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."



So, it does neither substitute shell variables, nor does it save loop iterations.



For this you could use bash debugging by calling your script so:



bash -x myscript > to-my-log-file


Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).






share|improve this answer




















  • ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
    – Richard
    Sep 5 at 17:45














up vote
1
down vote













The "history" is a history of "previously typed" commands.



From the GNU manual:



"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."



So, it does neither substitute shell variables, nor does it save loop iterations.



For this you could use bash debugging by calling your script so:



bash -x myscript > to-my-log-file


Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).






share|improve this answer




















  • ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
    – Richard
    Sep 5 at 17:45












up vote
1
down vote










up vote
1
down vote









The "history" is a history of "previously typed" commands.



From the GNU manual:



"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."



So, it does neither substitute shell variables, nor does it save loop iterations.



For this you could use bash debugging by calling your script so:



bash -x myscript > to-my-log-file


Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).






share|improve this answer












The "history" is a history of "previously typed" commands.



From the GNU manual:



"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."



So, it does neither substitute shell variables, nor does it save loop iterations.



For this you could use bash debugging by calling your script so:



bash -x myscript > to-my-log-file


Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 5 at 15:19









nst0022

1062




1062











  • ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
    – Richard
    Sep 5 at 17:45
















  • ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
    – Richard
    Sep 5 at 17:45















ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
– Richard
Sep 5 at 17:45




ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
– Richard
Sep 5 at 17:45

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466987%2fhistory-inside-a-bash-loop%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?