Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicate:
Can history files be unified in bash?

I use Ubuntu Server 9.10 and I would like to be able to see my bash history for more than one terminal sessions. I.e. my last 200 commands or so, even if I have been logged out in between.

When I use the history I just see all commands from my actual terminal session. How can I see more command history from Bash? Is there any specific settings for bash that I should change from the default values in Ubuntu?

I don't have a ~/.bash_history file. But I have an ~/.bashrc with HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups and HISTCONTROL=ignoreboth

echo $HISTFILE
/home/sanoj/.bash_history
echo $HISTSIZE
500
echo $HISTFILESIZE
500
echo $HISTCONTROL
ignoreboth

UPDATE: I am now trying Ubuntu Server 10.10 in VirtualBox. If I just turn off VirtualBox without the shutdown command, then next time when I boot, the commands from the last session is not saved in the history file.

The commands are only saved if I shutdown the machine with the shutdown command. E.g. shutdown -P 0.

This must be the reason to my problem. So I have to figure out how to save the command-history more often. E.g. after each command.

share|improve this question

marked as duplicate by quack quixote Apr 4 '10 at 12:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What do you get if you do echo $HISTSIZE and echo $HISTFILESIZE ? My (Ubuntu) ~/.bashrc has the same HISTCONTROL lines as yours which is brain dead since the second one overrides the first and the values are supposed to be colon-separated, not comma-separated. If you do set -o does it show a line that says "history on"? –  Dennis Williamson Mar 18 '10 at 22:51
    
I get 500 with echo on both variables and history on –  Jonas Mar 18 '10 at 23:22
    
the second HISTCONTROL statement is the one you're getting, not the first. though ignoreboth is bash shorthand for ignoredups + ignorespace so that probably isn't the issue. –  quack quixote Mar 19 '10 at 10:05
1  
this is a duplicate: superuser.com/questions/37576/… –  warren Apr 2 '10 at 20:07
1  
@warren: nice find. i agree, looks like exactly what Sanoj is asking for. –  quack quixote Apr 4 '10 at 12:32

4 Answers 4

up vote 3 down vote accepted

You can use

history -a

to immediately append the in-memory history to the history file. One terminal session can't see another's unless this is done or the other is exited.

You can use

history 200 | less

to see that number of entries.

In addition to HISTSIZE see the entry in the Bash man page concerning HISTFILESIZE.

share|improve this answer
    
history -a´ doesn't do anything for me, and not history 200 | less` either, but I have updated my question with more info. Thanks anyway. –  Jonas Mar 18 '10 at 22:35
    
I think that I have found a reason now. The history is not saved unless I do a clean shutdown. See my new question: unix.stackexchange.com/questions/3330/… –  Jonas Oct 20 '10 at 22:02

Once you log out the history gets appended to the file ~/.bash_history. Have a look in there.

By default it will remember your last 500 commands. If you want to save more set the variable HISTSIZE in ~/.bashrc.

I also do add ignoredups to HISTCONTROL (with HISTCONTROL=$HISTCONTROL:ignoredup). This makes duplicate consecutive commands to be save only once.

Have a look at man 1 bash for what else you can tune about the history.

share|improve this answer
    
Thanks for your reply. But I don't have an ~/bash_history file, and no HISTSIZE in ~/.bashrc but I have HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups and HISTCONTROL=ignoreboth and It is Ubuntu Server, if that matter. –  Jonas Mar 18 '10 at 22:36
1  
@Snoj Notice the . in ~/.bash_history. And for HISTSIZE it will just use the default value of 500 entries. –  Benjamin Bannier Mar 18 '10 at 22:47
    
Yes, but I have no ~/.bash_history file on my system. I used ls -a to check. –  Jonas Mar 18 '10 at 23:23
    
@Sanoj So maybe it is not the default. The filename is stored in the $HISTFILE variable, so echo $HISTFILE will tell you where to look. –  Benjamin Bannier Mar 18 '10 at 23:54
    
echo $HISTFILE gives me /home/sanoj/.bash_history - strange. Maybe it's not activated by security reasons? –  Jonas Mar 19 '10 at 8:20

Sanoj, this really should "just work"; I don't think there is supposed to be anything special you need to do to enable this. It sounds like something is interfering with the normal course of events.

I would look in /etc/profile, /etc/bashrc (or perhaps /etc/bash.bashrc according to some sources), ~/.bash_profile, ~/.bashrc, ~/.bash_login, ~/.bash_logout, to see if there is anything that might be affecting the history (perhaps grep -i hist on each of the above files).

... In particular I wonder if you have something in ~/.bash_logout that removes the file.

Alternatively, is it possible that something is overriding the $HOME variable?

share|improve this answer

bash history is usually loaded when the shell begins running, and is saved when it is exited normally. You can use history -a and history -n to override this, but not automatically unless you abuse $PROMPT_COMMAND or something similar.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.