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?
for i in {1..5}; do history -s echo $i; done
seehelp history
– Jetchisel Apr 17 '20 at 10:29echo
commands are not added because the command being run is the compoundfor
loop command (which is added to the history). It has nothing to do with subshells. – Kusalananda♦ Apr 17 '20 at 10:39echo 1; echo 2;
in one line is a single command in history, too ... – pLumo Apr 17 '20 at 10:44ignoredups
in$HISTCONTROL
. – Arkadiusz Drabczyk Apr 17 '20 at 22:39