Skip to main content
edited tags
Link
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324
added 154 characters in body
Source Link
Lavya
  • 1.7k
  • 5
  • 23
  • 28

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

Edit: Given it takes the entire for loop as one single compound command, if I remove the for loop and write the individual commands in five lines shouldn't all five be added to history?

I did the following:

#!/bin/bash set -o history var=5 echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) 

and ran the script using source. Still in history it shows only one echo statement echo $((var--)) and not all five. On the other hand, if instead of five echo $((var--)) I put five echo 5, echo 4, ... echo 1 commands all five are added to history.

Why?

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

Edit: Given it takes the entire for loop as one single compound command, if I remove the for loop and write the individual commands in five lines shouldn't all five be added to history?

I did the following:

#!/bin/bash set -o history var=5 echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) 

and ran the script using source. Still in history it shows only one echo statement echo $((var--)) and not all five. Why?

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

Edit: Given it takes the entire for loop as one single compound command, if I remove the for loop and write the individual commands in five lines shouldn't all five be added to history?

I did the following:

#!/bin/bash set -o history var=5 echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) 

and ran the script using source. Still in history it shows only one echo statement echo $((var--)) and not all five. On the other hand, if instead of five echo $((var--)) I put five echo 5, echo 4, ... echo 1 commands all five are added to history.

Why?

added 551 characters in body
Source Link
Lavya
  • 1.7k
  • 5
  • 23
  • 28

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

Edit: Given it takes the entire for loop as one single compound command, if I remove the for loop and write the individual commands in five lines shouldn't all five be added to history?

I did the following:

#!/bin/bash set -o history var=5 echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) 

and ran the script using source. Still in history it shows only one echo statement echo $((var--)) and not all five. Why?

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

On the bash terminal if I execute the following for loop:

 for i in {1..5}; do echo $i; done 

The echo commands are not added to the history of the current shell. Is it because the commands in for (in do section) are run in subshell? If so is there a way to run them in the current shell instead? Or is it not possible because it's "running" for at the time?

Edit: Given it takes the entire for loop as one single compound command, if I remove the for loop and write the individual commands in five lines shouldn't all five be added to history?

I did the following:

#!/bin/bash set -o history var=5 echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) echo $((var--)) 

and ran the script using source. Still in history it shows only one echo statement echo $((var--)) and not all five. Why?

Modify title to better reflect question
Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading
Source Link
Lavya
  • 1.7k
  • 5
  • 23
  • 28
Loading