If you use a Mac or Linux terminal, chances are you’ve interacted with the shell history. Most people I know just use the up arrow to find a previously executed command, but this misses out on a serious amount of power.

In this tutorial, I’m going to teach you how to navigate history like a boss.


What Exactly is History?

Most Unix-style shells come with a history feature built in. This means when you type a command it is remembered by the shell and you can recall it later.

History isn’t magic, the commands are actually stored in memory and subsequently in a file. You can locate this with:

echo $HISTFILE

For me this prints:

/home/dir/.zhistory

To view the file contents we can run:

cat $HISTFILE

We can see this is just a flat list of commands. The number of commands stored is governed by two shell variables:

  • HISTSIZE, which determines how many commands are stored in memory while a session is running
  • HISTFILESIZE, which determines how many commands are stored in your history file.

To save the last 1000 commands, add something like the following to your shell config file (usually ~/.bashrc or ~/.zshrc):

export HISTFILESIZE=1000
export HISTSIZE=1000

#bash #shell #unix #linux #programming

Navigate Your Unix Shell History Like a Boss
1.45 GEEK