Skip to content
ImagineWorld

Using Shell History

node_idnode-book_the-software-developer-s-guide-to-linux_chapter-03-using-shell-history
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num3
chapter_titleUsing Shell History
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

To become skilled at the command line, you have to use it regularly. There are no shortcuts to becoming comfortable, but there are a few high-value tricks that you can learn early on, which will save you time and frustration. The earlier you build them into your muscle memory, the better.

In this chapter, you’ll learn how to leverage your shell history to avoid the tedious re-typing of commands you’ve already run. You’ll also see how to customize your shell’s behavior or appearance via the shell configuration file. Finally, we’ll show you the most useful shortcuts to edit and modify commands at the command prompt. All in all, this chapter will make you blazing fast on the command line.

We will go about this by covering the following topics:

  • Shell history
  • Executing previous commands with !
  • Jumping to the beginning or end of a line

Let’s begin by understanding shell history.

Most shells keep a history of the commands you’ve run. This means that you can see every successful command that you’ve run just by pressing the arrow keys: the Up-Arrow key to go back one command and Down-Arrow to go forward one. Scrolling through your shell history like this can be very useful, especially if you find that you’re re-running similar commands frequently.

Note that you can also edit commands that you find like this: use Left-Arrow and Right-Arrow to navigate to the line of text that is a command, and just type to edit the command.

An edited command is added to the end of your shell history (it doesn’t actually modify the saved line in the history).

Together, these tricks allow you to easily go back and re-execute or modify previous commands.

Some of the tricks we’ll talk about require changes to your shell’s configuration file. The workflow is usually the following:

  1. Change the option you want to change in your shell’s config file.
  2. Save the file.
  3. Open a new shell session to see the changes.
  4. For existing shell sessions, re-read your shell config file by running a command to source (execute) it: source ~/path/to/config/file.

Here are locations for the most common shells:

Common shellsLocations
Bash~/.bashrc for interactive sessions, such as the one you get from opening a new terminal window in your graphical environment. You almost always want this one if you’re changing the configuration on your work machine. “Interactive” here refers to situations where you as a user make use of the shell in a terminal, not to a script running (for example, a script automatically invoked by a cron job). You’re in an “interactive shell” when you are in some form of terminal, manually being prompted for input and writing commands. ~/.bash_profile for login shells – this might be a local login, but also what you get if you log in over SSH. Again this is in comparison to shell instances when running a script.
Zsh (the Z shell)~/.zshrc

Different shells keep history files in different places, and most can be configured to change the location. By default, you’ll almost always be using Bash, which, by default, keeps its history file at ~/.bash_history.

If you’re ever unsure about where to find the shell history file, many shells have a shell configuration option named HISTFILE that contains the history file’s location.

Here, I’m checking to see where my history file is, while running the zsh OS:

% echo $HISTFILE
/home/dcohen/.zsh_history

Bash has two configuration options that prevent your history file from growing indefinitely, to keep its size manageable and your history searches fast:

  • HISTSIZE controls the maximum history kept in memory
  • HISTFILESIZE controls the maximum size of the history file that’s saved between shell sessions

If you want to increase the amount of history that Bash keeps, increase both of the preceding in your shell’s configuration file.

To do so open the shell’s configuration file (e.g., ~/.bashrc) and set these variables by appending the following lines to the end of the file:

export HISTSIZE=1000
export HISTFILESIZE=5000

You will often find yourself looking for a command you ran a week (or a month) ago. That command will likely be further back in your history, and it would be a waste of time to hit Up-Arrow hundreds of times to get there. If you have at least some idea of what you’re looking for, an interactive shell history search is the trick that you’re looking for. Here’s how to search your shell history:

  1. Press CTRL + R to invoke reverse-i-search.
  2. Type a part of the command you are looking for.
  3. Your shell will try to match the characters you type against your command history and come up with the closest, most recent match.
  4. Repeatedly press CTRL + R to step through the history. Press ENTER to select a command or Esc to exit this mode.
  5. If you accidentally skip backward past the command you wanted, CTRL + SHIFT + R will search forward to the next-most-recent match.

There are some exceptions to this feature depending on the shell and the configuration you are using.

Some shells forget about commands that failed with an error (exited with a non-zero exit code). Many shells also forget commands starting with a space character – those won’t be added to the shell’s history. However, in both scenarios you are typically still able to reach the history entry if you go back (Up-Arrow) right away, without executing any other commands.

Executing previous commands is done with exclamation marks. There are various ways to use this trick, which we’ll look at now.

Re-running a command with the same arguments

Section titled “Re-running a command with the same arguments”

The ! command will execute the last command with the previous arguments. For example, !ssh will go back and find the last ssh command you ran and execute it with the same arguments. You can use this to re-run commands that you frequently use with the same arguments, such as to quickly re-connect to the SSH server you connect to every day.

Prepending a command to something in your history

Section titled “Prepending a command to something in your history”

The !! command will execute the last command you ran, but with some other command in front of it. This may sound strange, but it’s very useful for situations where you accidentally ran a command that requires root privileges without sudo at the beginning.

apt-get install nginx # fails with a permission error
sudo !!
# this is the command that runs:
sudo apt-get install nginx

After that previous command fails due to a lack of permissions, simply running sudo !! will re-run it with sudo prepended to the beginning.

Note

For security, don’t make this an automatic habit: always make sure you know why a command needs more permissions and ask yourself whether you trust it enough to want to give it permission to do literally anything on your system. Careless misuse of sudo can make it easy to break things or allow an attacker to gain a foothold on your system.

Jumping to the beginning or end of the current line

Section titled “Jumping to the beginning or end of the current line”

It’s not uncommon to need to jump to the beginning of a line when editing, perhaps to correct the spelling of a command or add a required argument. To do this, press CTRL + A.

Likewise, to jump back to the end of a line, use CTRL + E.

These two will come in handy quite often.

Working in a Linux shell environment entails a lot of typing. Making even the smallest improvements to your speed and accuracy as you construct and edit your commands can make the difference between feeling like basic tasks are taking forever and feeling like you’re speeding along like a seasoned Unix wizard.

The tricks we’ve shared in this chapter are a few of the most common and most powerful shortcuts we use in our day-to-day work. Combining your new command-history-searching skills with the editing and command-modification shortcuts you just learned will have a massive impact on your comfort, efficiency, and speed on the command line.