Here is my HISTFILE:$ echo $HISTFILE/home/Steven/.bash_historyI can create it by using exit:$ [ -e $HISTFILE ]; echo $?1$ exit$ [ -e $HISTFILE ]; echo $?0However if I exit a subshell, it is not created:$ [ -e $HISTFILE ]; echo $?1$ (exit)$ [ -e $HISTFILE ]; echo $?1Why is the history file not getting created in a subshell?
Create HISTFILE in subshell
bash;shell;command history
If a bash shell is opened, it is passed the content of the history file. When it exits, it appends its history to the history file.If you open another (interactive) bash shell (in the same terminal window), execute a few commands or only exit, the commands (or only exit) will be appended to the history file.If you however run a non-interactive sub-shell, history is turned off and environment variable HISTFILE will be unset.You can verify that by running (bash -c 'echo x$HISTFILE') or just bash -c 'echo x$HISTFILE'. This will only echo x, proving that variable HISTFILE is unset in the sub-shell. (I've only added the x to have some visible output from the echo command.)(The reason for bash -c and the single quotes it to prevent shell variable expansion in the parent shell. If you run (echo x$HISTFILE), shell variable expansion will pass the value of HISTFILE to the sub-shell, which it then echos, making it look like it is set in the sub-shell).So when running a sub-shell, history is turned off and variable HISTFILE is unset. When the sub-shell exits, nothing is appended to the history.  That is exactly what you are seeing.You can force the sub-shell to write its history to the history file:$ history -w  # write current history$ wc -l $HISTFILE  # count lines in history file32 /home/anyuser/.bash_history$ bash -c 'export HISTFILE=~/.bash_history'$ wc -l $HISTFILE32 /home/anyuser/.bash_history$ bash -c 'export HISTFILE=~/.bash_history;history -w'$ wc -l $HISTFILE0 /home/anyuser/.bash_history The last sub-shell command (the one with history -w) overwrites the existing history file with the history of the sub-shell, which is empty because history is turned off in a non-interactive sub-shell.