midnight commander subshell - sharing a history file with the shell mc was started from

This could be done with the following command (before starting mc):

export PROMPT_COMMAND="history -a; history -r"

If you would like to make it permanent, then you should add it to your ~/.bashrc. But it will make all shell history global, which is not the best. If you don't want this side effect, then the following line should be added instead of the above one:

alias mc='PROMPT_COMMAND="history -a; history -r" mc; history -r'

Explanation:

PROMPT_COMMAND is executed just before bash displays a prompt. Further reading here.

history should be called with append parameter, and after that with read parameter. Further reading here.


For some reason, the PROMPT_COMMAND solution in an mc alias, as suggested by user szkj, didn't work correctly for me. While it did preserve commands executed in the MC subshell, commands entered before starting MC were lost after exiting.

Finally, I use the following in ~/.bashrc, without an mc alias. It seems to work fine despite the fact that mc overwrites $PROMPT_COMMAND.

export HISTCONTROL=ignoreboth
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

This is what I get:

# echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
1: before starting MC. PROMPT_COMMAND=history -a; history -c; history -r; 

# mc

# echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
2: Inside MC. PROMPT_COMMAND=pwd>&8;kill -STOP $$

# history | tail -3
 3038 echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3039 echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3040 history | tail -3

# echo "3: Going to exit MC with F10."
3: Going to exit MC with F10.

# echo "4: Back in Bash after MC"
4: Back in Bash after MC

# history | tail -7
 3038 echo "1: before starting MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3039 echo "2: Inside MC. PROMPT_COMMAND=$PROMPT_COMMAND"
 3040 history | tail -3
 3041 echo "3: Going to exit MC with F10."
 3042 mc
 3043 echo "4: Back in Bash after MC"
 3044 history | tail -7

My system is:

# cat /etc/debian_version 
8.1

# bash --version | head -1
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)

# mc --version | head -1
GNU Midnight Commander 4.8.13

# uname -a
Linux afnet 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux