How To Use Bash History Commands and Expansions on a Linux VPS

Introduction:

A strong and adaptable Bash History Commands and Expansions on a Linux VPS  is the bash shell. It offers an extensive range of features and functionalities, one of which is the capacity to monitor your command history. Gaining knowledge of Bash history commands and extensions can greatly increase your efficiency and productivity when using the terminal. We will go into great detail about these features in this article and give you a thorough grasp of how to use and navigate your command history.

Table of Contents:

  1. Understanding Bash History
  2. Viewing Command History
  3. Running Previous Commands
  4. Searching Command History
  5. Navigating History with Keyboard Shortcuts

 

Understanding Bash History

With the help of a feature called “bash history,” the shell can compile and save a list of all the commands you’ve run in order during your terminal sessions. By default, this history is kept in a file called ~/.bash_history. Because every command has a timestamp attached to it, you can effectively remember, repurpose, and manage your command history. Bash History Commands and Expansions on a Linux VPS is very important in linux.

Viewing Command History

To view your command history, you can use the history command. Simply open your terminal and type:

history

This command will display a numbered list of your command history, starting with the most recent command at the bottom. Each command entry is prefixed with a unique number that can be used to reference and run specific commands.

By default, the history command displays the last 1000 commands, but you can change this limit by modifying the HISTSIZE environment variable in your shell configuration file (usually ~/.bashrc or ~/.bash_profile).

Running Previous Commands

Running previous commands from your history is straightforward, and there are several methods to do it:

  • Using the !! symbol: The double exclamation mark (!!) is a shortcut to run the last command. For example:
    $ !!

    This will re-execute the previous command.

  • Using command numbers: As mentioned earlier, each command in your history is associated with a unique number. To rerun a specific command, use the ! followed by the command number. For example:
    $ !123

    This will execute the command with number 123 from your history.

  • Using negative numbers: To run commands relative to the most recent entry, you can use negative numbers. For instance, -1 refers to the last command, -2 to the second-to-last, and so on.
    $ !-2
  • Searching and running commands by text: If you remember a part of a previous command, you can search your history using the CTRL+R keyboard shortcut. It will initiate a reverse search, and you can start typing a portion of the command. Press ENTER to execute the found command.

Searching Command History:

One effective method to locate and execute particular commands again without having to recall their precise sequence number is to search your command history. As previously indicated, you may perform a reverse search by pressing CTRL+R. For more involved searches, however, you can also use the history command in conjunction with grep.

This is an illustration of how to look for commands that contain the term “git”:

$ history | grep git

This command will show you a history of all the commands you’ve run that contain the word “git.” Based on the context, you may then select a command to run again.

Navigating History with Keyboard Shortcuts

Bash provides several keyboard shortcuts that make navigating your command history quick and efficient:

  • Up and Down Arrow Keys: Use the up and down arrow keys to cycle through your command history one entry at a time.
  • CTRL+P and CTRL+N: These keyboard shortcuts are equivalent to using the up and down arrow keys to navigate your history.
  • CTRL+R: As mentioned earlier, CTRL+R initiates reverse search, allowing you to find and rerun commands by typing a portion of the command.
  • ALT+. (ALT key plus period): This shortcut retrieves the last argument from the previous command. It’s particularly useful when you want to reuse a file or directory path from a previous command.

 

History Expansion

The extension of the bash history lets you edit and reuse some of your old commands. It’s a strong tool that can help you save time and keystrokes. Here are a few typical methods for adding more history:

  • !!: Repeats the last command.
    $ ls -l
    $ !!

    The second command will repeat the ls -l command.

  • !$: Refers to the last argument of the last command.
    $ cp file.txt /path/to/destination/
    $ mv !$ new-destination/

    The second command will move file.txt to the new-destination directory.

  • !:n: Refers to the nth argument of the last command. For example, !:1 is the first argument, !:2 is the second argument, and so on.
  • !^: Refers to the first argument of the last command.
    $ echo apple banana cherry
    $ cat !^

    The second command will display “apple.”

  • $: Refers to the last argument of the current command line.
    $ mv file.txt !$

    This will move file.txt to the directory specified in the current command line.

  • !*: Refers to all arguments of the last command.
    $ ls -l file1.txt file2.txt file3.txt
    $ chmod +x !*

    The second command will add execute permission to all the listed files.

  • !!:n: Refers to the nth argument of the last command.
  • !!:n-m: Refers to a range of arguments. For example, !!:2-4 refers to arguments 2 to 4.
  • !string: Searches your history for a command that starts with “string” and executes it. This is particularly useful when you want to rerun a specific command from your history that you don’t remember by number.
    $ !grep

    This command will execute the most recent command that starts with “grep.”

Conclusion:

You need to be well knowledgeable about how to take use of the historical operations at your disposal after reading this book. While some of these features will likely be more beneficial than others, it’s still nice to know that bash has them in case you ever find yourself in a situation where it would be advantageous to look them up.

The history command by itself, the reverse search, and the fundamental history extensions may all significantly expedite your process.

 

Get more information about
How to Clean Install NVIDIA Drivers on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *