Bash History | Linux

2024/01/03

Bash records the commands you type, providing a convenient way to view and reuse them. Users can interact with this history using various commands and shortcuts.

Understanding Bash History:

Bash History keeps a record of the commands a user has entered during a session. Each command is assigned a unique number, allowing users to easily review, modify, and reuse previous commands. This feature is especially valuable when dealing with lengthy or complex commands, as it eliminates the need to retype them.

Key Features of Bash History:

  1. Command Recall: Users can retrieve previous commands by referencing their assigned number or by searching through the history.
  2. Command Modification: The history facilitates command modification through reverse search or direct editing of the command line.
  3. Efficiency and Productivity: By leveraging Bash history, users can optimize their workflow, saving time and reducing repetitive typing.

Improving Command Line Skills:

Becoming proficient with Bash history helps improve your command line skills. Learning how to navigate, search, and manipulate command history enables users to work more efficiently and handle various tasks with ease.

Commands:

Users can interact with the history using various commands and shortcuts, including:

  • Up/Down Arrow Keys: Browse through previously entered commands.
  • !! (Double Exclamation Mark): Re-run the last command.
  • !n (Exclamation Mark Followed by a Number): Execute the command at a specific position in the history.
# View command history (last 10 entries)
history 10
tail ~/.bash_history
## Note: This depends on which shell you are using
echo $SHELL
## If your shell is zsh, you should use this command instead
tail ~/.zsh_history

# Re-run the last command
!!

# Execute the command at position 5 in the history
!5