Bash history commands

Bash history commands

Study notes for Bash about history commands.

Bash history commands allow you to easily manage, recall and reuse previous commands you have entered in a Bash terminal. Some of the main things you can do with Bash history commands are:

• Recall previous commands - Using commands like !number, !! and ctrl + r, you can easily recall previous commands from your history and re-execute them.

• Edit previous commands - You can recall a command using !string or ctrl + r and then edit it before executing. This allows you to reuse similar commands with small changes.

• Search history - Using ctrl + r, you can search your history for a specific command you previously entered.

• Repeat commands - The !! command will repeat the immediately previous command, allowing you to quickly re-execute a command.

• Navigate history - You can navigate forwards and backwards through your history using the up and down arrow keys.

• Save history - By default, Bash saves the last 500 commands in your history. You can change this using the HISTSIZE variable.

• Clear history - Using history -c, you can clear your entire command history.

These features make Bash history commands very useful for productivity and convenience. They allow you to quickly recall and reuse previous commands, saving time and effort.


History Commands

Here are some commonly used bash history commands:

  1. history - Shows the full history list of commands that you have entered.

  2. !number - Executes the command with that number from the history list. For example, !5 will execute the 5th command from the list.

  3. !string - Executes the most recent command that starts with that string. For example, !ls will execute the most recent ls command.

  4. !$ - Refers to the last argument of the previous command.

  5. !! - Repeats the last command.

  6. ctrl + r - Allows you to search through the history for a command. Start typing and it will find the most recent matching command.

  7. history -c - Clears the entire history list.

  8. HISTSIZE - Controls the number of commands stored in the history list.

These commands make it easy to navigate your bash command history, recall previous commands, repeat commands and search the history list.


Use-Cases

Here are some use-case examples of using history commands in Bash scripts:

  1. Repeating a command periodically:
while true; do 
    !! 
    sleep 60 
done

This will continuously repeat the last command you ran, waiting 60 seconds in between executions.

  1. Running a series of similar commands:
!5
!6 
!7
!8
!9

This will run commands 5 through 9 from your history, allowing you to quickly execute a series of similar commands.

  1. Searching for and executing a command:
grep -i string | while read line; do 
    !$line
done

This will search your history for commands containing "string" and then execute each matching command.

  1. Creating an alias from a previous command:
alias alias_name='!100'

This will create an alias named "alias_name" that executes command 100 from your history.

  1. Debugging a previous command:
!! 
echo "Adding debug flag"
!! -d

This will re-execute the last command, echo a message, and then re-execute that same command again with a "-d" debug flag appended.

These examples show how powerful history commands can be when used programmatically in Bash scripts. You can repeat commands, search for commands, modify commands, and even create aliases - all based on your previous command history.


Disclaim: Created with ChatGPT.