In my current setup, all of my bash histories are stored in a single file, which is pointed to HISTFILE.

This history includes a history of the host system and all containers.

Along with this history, I want to set up per container bash_history_<cont_name>.

Setup Details:

  • The host system has a docker containers setup.
  • All containers have /home/uname/ shared with the host. (.bashrc is also shared)
  • All command history [ of host & all containers ] is written to HISTFILE.
  • For quick updates to HISTFILE, the prompt is updated in .bashrc in the following way.
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Along with this system in place. I also want to save the history of commands used in containers to file dedicated to that container only.

Ex. For containers ContA & ContB, the command history will be saved to common 'HISTFILE', as well as command history of respective containers should also be updated. For ContA, ~/bash_history_ContA should also be updated.

P.S.: The single file command history is updated as soon as the command is executed. The bash_history_<cont_name> per container could be a lazy updated file.

  • What containers? Please edit your question and give us more details about your setup. It is relatively easy to have a script that changes your HISTFILE depending on something, for example on the hostname so you can have different histories on different hostnames even if you share the same $HOME on all of them, but we need to know what we can use to know we need to switch in your case.
    – terdon
    Commented Apr 18 at 9:43

1 Answer 1


For the "The single file command history is updated as soon as the command is executed", you can do this with one of the 3 answers to this question (test the one that works best for you)

In the PROMPT_COMMAND you can also add any redirection to whatever global file you wish : just add the latest command to it before each prompt ? (careful : some may be multiline)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .