How to use Bash history commands

In Bash, the command history is a useful feature that allows you to view and reuse previously executed commands. The history is stored in a file (usually ~/.bash_history), and you can interact with it using various commands and shortcuts.

The history command

The history command in Bash is used to display the contents of your command history list. By default, it shows the last 1000 commands that you have executed. This command provides a convenient way to review your command history, allowing you to see a list of previously entered commands along with their line numbers. With this information, you can easily rerun specific commands using their line numbers or by employing various shortcuts and event designators.

The command history is a valuable feature in Bash, facilitating command recall and reuse, enhancing overall command-line efficiency. Additionally, you can customize the history behavior, such as adjusting the number of commands stored, by modifying the HISTSIZE and HISTFILESIZE environment variables in your Bash configuration file, typically located at ~/.bashrc.

Options:

  1. -a:Append to the history file instead of overwriting it.
  2. -c:Clear the history list.
  3. -d <offset>:Delete the command at the specified offset (starting from 1).
  4. -n <number>:Display only the last number entries.
  5. -r:Reverse the order of history entries.
  6. -w <filename>:Write history to the specified file.

Here are some details with examples:

Viewing Command History

To view the command history, you can use the history command:

history

This will display a list of previously executed commands along with their line numbers.

-a: Append to the history file instead of overwriting it

history -a

This appends the current session's history to the existing history file, preserving past commands.

-c: Clear the history list

history -c

This removes all commands from the current history list, preventing them from being accessed by subsequent history commands.

-d <offset>: Delete the command at the specified offset

history -d 5 # Deletes the 5th command from the history list

This removes a specific command from the history, indicated by its offset (position) starting from 1.

-n <number>: Display only the last number entries

history -n 10 # Displays only the last 10 commands

This shows a limited number of recent commands, as specified by the <number> argument.

-r: Reverse the order of history entries

history -r

This displays the history list in reverse chronological order, starting with the most recent command.

-w <number>: Write history to the specified file

history -w my_history.txt

This saves the current history list to a file named my_history.txt, allowing you to store or transfer it.

The fc Command

To recall, modify, and reuse commands from your history in Bash, the versatile fc command is at your disposal. It offers various options for listing, editing, and executing past commands, enhancing your command-line efficiency. The syntax follows either fc [-lnr] <number> or fc -e <number>, where each flag serves a specific purpose.

  1. -l:List the specified command's history entry.
  2. -n:Execute the specified command again.
  3. -r:Reverse the order of history entries (similar to -r in history).
  4. -e:Edit the specified command with your default text editor.

-l: List the specified command's history entry

# Let's assume "ls -l /home" was your 15th command in history. fc -l 15

This shows the exact text of the 15th command ("ls -l /home") without executing it.

-n: Execute the specified command again

# Assuming your 10th command was "cd documents", running this executes it: fc -n 10

This directly re-runs the command identified by the position (10 in this case), saving you from retyping it.

-r: Reverse the order of history entries

fc -r

This displays your history in reverse chronological order, with the most recent command at the top. It's similar to history -r but offers additional fc options on the same output.

-e: Edit the specified command with your default text editor

# Let's edit the 20th command ("grep error log.txt"): fc -e 20

This opens the 20th command ("grep error log.txt") in your default text editor for modifications before you re-run it with the desired changes.

Keyboard shortcuts

  1. Up/Down arrow keys:Navigate through history.
  2. Ctrl+P/Ctrl+N:Navigate through history one page at a time.
  3. Ctrl+R:Reverse search for a previous command.
  4. Alt+Shift+. (period): Go to the end of history.
  5. Alt+Shift+, (comma): Go to the beginning of history.

Search and filtering

  1. Pipe history to grep or less to filter commands based on text patterns.
  2. Use Ctrl+R for interactive reverse search.

Environment variables

  1. HISTFILE: Specifies the history file location (defaults to .bash_history).
  2. HISTSIZE: Sets the maximum number of history entries (defaults to 1000).
  3. HISTFILESIZE: Sets the maximum size of the history file in kilobytes (defaults to 4096).

Exclamation mark (!) in Bash

The exclamation mark ! in Bash serves as a powerful tool for recalling and interacting with your past commands.

Executing Commands from History

You can rerun a command from history using the ! (exclamation mark) followed by the command's line number. For example:

!123

This will rerun the command at line 123 of the history.

Alternatively, you can use negative numbers to reference commands relative to the current command:

!-2 # Re-run the second-to-last command

Executing Commands by Prefix

You can rerun the most recent command that starts with a specific prefix using ! followed by the prefix.

!ls

This will execute the most recent command starting with "ls."

Search and Execute Commands

You can search for a command in the history using Ctrl + R and then start typing part of the command. Bash will autocomplete based on your input. Press Enter to execute the selected command.

Repeating Commands

You can repeat the last command by simply typing !!:

!!

You can also repeat the last command that started with a specific prefix:

!ls

Event Designators

Bash supports event designators to refer to specific events in the command history. Some examples include:

!$ refers to the last argument of the last command. !^ refers to the first argument of the last command. echo one two three echo !$ # Outputs: three

History Expansion

You can use history expansion with ! to manipulate and reuse parts of previous commands. For example:

echo !:2 # Refers to the second argument of the last command

Using Bash history effectively

  1. To avoid cluttering your history, regularly clear it or adjust HISTSIZE and HISTFILESIZE.
  2. Consider exporting history or importing it to share or transfer commands across systems.
  3. Customize your HISTFILE and HISTIGNORE (to ignore specific commands) based on your preferences.

Conclusion

Bash, a popular Unix shell, maintains a command history that records previously executed commands. Users can navigate and reuse commands efficiently using features like the command history, which stores a sequential list of past commands for enhanced command-line productivity.