Your Command History

The shell remembers every command you type, so the up arrow brings back the last ones, history lists them all with numbers, Ctrl+R finds one by any part of it, and clear gives you a fresh screen.

Apprentice · Unix · Linux · Updated

After this lesson you can

  • Bring back and rerun earlier commands with the up arrow and history
  • Find an earlier command with Ctrl+R, and clear the screen with clear

Key takeaways

  • The up arrow brings back your last command, ready to run with Enter or to change first. Press it again for older ones.
  • history lists the commands you've typed, numbered, oldest first. history 5 shows only the last five.
  • Ctrl+R searches your history as you type part of a command. Enter runs the match, and Ctrl+C gives up.
  • bash saves your history in ~/.bash_history when the terminal closes, so tomorrow's terminal remembers today's commands.
  • clear (or Ctrl+L) wipes the screen. It doesn't erase your history.

Ten minutes ago you typed a long command from a tutorial, and it worked. Now you need it again, with one word changed. Or you set up a practice folder yesterday, and today you can't remember the exact path. Typing it all again is slow, and every letter is a chance for a typo. You don't have to: the shell keeps a list of every command you've typed, and it takes one key to get them back.

The examples start in a new terminal and run three commands in the practice folder from Reading Files: cat and less:

cd ~/terminal-practice/recipe-box
cat settings.conf
file CHANGELOG.md
port=8080
theme=light
language=en
shopping_list=on
CHANGELOG.md: ASCII text

The up arrow: your last command, back again

Press the up arrow. Your last command appears at the prompt, as if you'd typed it, but it hasn't run yet:

$ file CHANGELOG.md

Press Enter, and it runs again. Press the up arrow a second time instead, and you get the command before that:

$ cat settings.conf

The down arrow goes the other way, back toward the newest. To change a command before running it, move along the line with the left and right arrows, fix what you need, and press Enter: bring back cat settings.conf, change settings.conf to README.md, and you've read another file without retyping cat. Changed your mind? Ctrl+C (hold Ctrl and press C) drops the line without running it, and shows ^C where you stopped:

$ cat settings.conf^C
$

history: the whole list

The up arrow is quickest for the last few commands. To see them all, type history:

history
    1  cd ~/terminal-practice/recipe-box
    2  cat settings.conf
    3  file CHANGELOG.md
    4  history

This list is your command history: every command, oldest first, numbered, including history itself. Yours will be longer, with bigger numbers. A number after it shows only the last few:

history 3
    3  file CHANGELOG.md
    4  history
    5  history 3

While the terminal is open, bash keeps the list in memory. When you close it, bash saves the list in a hidden file in your home folder, ~/.bash_history, so a new terminal tomorrow still remembers today's commands.

In zsh, the shell on a Mac, history shows only the most recent commands; history 1 shows the whole list, which zsh keeps in ~/.zsh_history.

The history saves everything you type on the command line, so never put a password in a command. When a program asks for your password and nothing shows as you type, as sudo does, that typing isn't saved.

Ctrl+R: search your history

Scrolling up through a hundred commands to find one is slow. Hold Ctrl and press R, and the prompt changes to a search:

(reverse-i-search)`':

Now type any part of the command you're looking for. Type set, and bash shows the newest command that contains it:

(reverse-i-search)`set': cat settings.conf

What to do next:

  • Enter runs the command it found.
  • Ctrl+R again finds the next older command that matches.
  • The right arrow (or Esc) puts the command on the prompt so you can change it first.
  • Ctrl+C gives up and gives you an empty prompt.

When nothing matches what you typed, the prompt starts with (failed reverse-i-search): take a letter away with Backspace, or try another word. The name means it searches in reverse, from newest to oldest, a letter at a time as you type. In zsh, Ctrl+R works the same way, and its prompt says bck-i-search.

clear: a fresh screen

After a while your screen fills with old output. Type clear and press Enter, or press Ctrl+L, and the screen is wiped, with the prompt at the top. Only the screen is cleaned: the up arrow, history and Ctrl+R still remember everything.

Try it in the terminal

Terminal Quest: The Lighthouse, goals 10 to 11: played right on the lesson page.

You've climbed to the top of the lighthouse tower. Read the keeper's logbook, look at your own with history, then light the lamp.

Check yourself

2 questions to try on the lesson page.

So what

Every command you've typed is one key away. The up arrow, history and Ctrl+R mean you type a long command once, and from then on you only fix the part that changes.

Key terms

Cheat sheets

Related