Kate's Comment

Thoughts on British ICT, energy & environment, cloud computing and security from Memset's MD

How to increase the Mac OSX bash shell history length – step by step

What is the bash shell history and why would I want it to be longer?

Every time I upgrade my Mac I seem to lose my bash shell history length settings. What is this, you may ask. I use the Terminal (under Applications->Utilities) a lot; everything from connecting to remote servers via ssh to moving files (I often make mistakes with Finder) to killing rogue processes (those apps that just won’t die, even with a “Force Quit”).

A lot of the stuff that I do from the shell is not really documented anywhere. I don’t mean the commands themselves are not perfectly well documented, just that the particular combinations I use are. For example, to log into one of my home servers when not at home I might use something like:

ssh -At kcw@home.craig-wood.com 'ssh -At root@ "screen -rx"'

I don’t want to have to type that out every time, so I rely on the shell history. You can access it most simply by pressing the up and down arrows, but I tend to use CTRL+R to do a reverse search. I then just need to type the first few letters of something distinct, eg. “home” in this case, and hit enter. This is what it looks like:

KatBook:~ kate$
(reverse-i-search)`home': ssh -At kcw@home.craig-wood.com 'ssh -At root@ "screen -rx"'

  • Tip: To cancel and escape from a reverse search without running the command it has found, just hit CTRL+C.

However, by default the Mac bash shell doesn’t record very many commands in its history. This means that the commands I might want to access soon get forgotten as I use the command line for other things. To avoid this one needs to increase the number of commands remembered in the history, which you do as follows.

How to increase your history

You’ll need to edit the “.bash_profile" file under your home directory. Files and folders starting with “.” are normally hidden, and since we’re talking about the command line I suggest you do it from there.

1) Open a terminal / shell window by going to Applications -> Utilities -> Terminal.
2) Open your .bash_profile for editing by entering the following at the command prompt:

nano -w ~/.bash_profile

  • nano” is a light-weight text editor.
  • The “-w” flag tells nano to not wrap lines. If you have a small window open you don’t want it inserting a new line / carriage return when it wraps!
  • The tilde (“~“) is a shortcut for the path, “/Users/<your-username>“. If you have previously been using Terminal it will open in whichever directory you were working on last, so you need to be explicit.

You should now be looking at the contents of your .bash_profile. It probably looks something like this:

export PATH

We want to edit the number after “HISTFILESIZE=“. Personally, I never really want my history to be forgotten since there always seems to be some ancient command I want to re-use so I set mine to 10000000.

2b) It is possible that you won’t have a .bash_history. In this case nano will still open but you’ll have a blank page. Don’t worry, just copy and paste the following into the window to create one:


You may like to choose a number other than the one above of course.

3) Now save the file by pressing CTRL+X then Y then ENTER.

Job done!

How to delete your bash history

Having greatly extended your bash history it is useful to know how to delete it. There are many scenarios in which you might want to do this. The history itself is stored as a simple file at “/Users/<your-username>/.bash_history".

If you are bothering to delete it then you probably want it really, absolutely, 100% gone. In that case you should use “srm” (Secure Remove), like so (from the terminal):

srm -m ~/.bash_history

The “-m” flag means use medium security which in this case means overwriting the file 7 times in a manner compliant with US Department of Defence standards, so you’re in good company!