What determines what shows up in the bash history command?

2

1

When I run history, I get a list of commands. However, it does not appear to be complete, even for a given period of time. (It doesn't feel like it has continuous coverage of the past N commands - it feels like some are missing.) Also, the output of history is different in different terminal tabs. Is this because the history isn't written to disk until the tab is closed? Does that explain why certain commands seem to get lost, because they are overwritten by closing tabs?

I'm on OSX 10.8 for what it's worth.

Nick Heiner

Posted 2013-01-14T18:45:57.517

Reputation: 1 382

Answers

5

Different terminals are running different instances of your $SHELL (which is BASH probably since you are on OSX). Each instance keeps its own history which is concatenated into the global session history when that particular BASH instance exits.

You can try this by opening two terminals, running some commands, then closing both terminals and opening a third. The commands from the (now closed) two previous terminals will now be in your history.

There are also some BASH options to remove duplicate commands, look at your $HOME/.bashrc and check if you have a line like:

export HISTCONTROL=ignoredups

That option tells BASH not to save duplicate commands. So, if you run ls 10 times, it will only be saved once.

terdon

Posted 2013-01-14T18:45:57.517

Reputation: 45 216

0

The shell history exists per instance of bash. It is read in when bash starts, and is written out when bash exits. Closing the tab forcibly kills bash, rendering it unable to write out the history as normal.

Ignacio Vazquez-Abrams

Posted 2013-01-14T18:45:57.517

Reputation: 100 516

What do you mean by 'closing the tab'? How should I end the session to let bash write the history? – Nick Heiner – 2013-01-14T20:20:29.217

You should use exit, or press Ctrl-D on an empty line. – Ignacio Vazquez-Abrams – 2013-01-14T20:22:37.080