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

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

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

Bash history is a feature that keeps a record of commands that have been executed on a Linux VPS. This can be useful when you need to re-run a command or if you need to remember a particular command that you used in the past. Bash also provides several expansion features that can help you quickly and easily reference previous commands.

Step 1: View Your Bash History

The first step is to view your Bash history. You can do this by typing the following command:

history

This will display a list of your most recent commands. Each command is preceded by a number, which you can use to reference the command in later steps.

Step 2: Re-run a Command

To re-run a command from your history, you can use the !n syntax, where n is the number of the command you want to re-run. For example, if you want to re-run the command with the number 1234, you would type:

!1234

This will re-run the command with the number 1234 from your Bash history.

Step 3: Use Command Substitution

You can use command substitution to reference the output of a previous command. To do this, you use the $(command) syntax, where command is the command you want to reference. For example, if you want to reference the output of the ls command, you would type:

echo "$(ls)"

This will print the output of the ls command to the console.

Step 4: Use History Expansion

Bash provides several expansion features that can help you quickly and easily reference previous commands. Some of the most useful history expansions are:

  • !! - Re-run the previous command
  • !$ - Reference the last argument of the previous command
  • !:n - Reference the n-th argument of the previous command
  • !^ - Reference the first argument of the previous command
  • !* - Reference all arguments of the previous command

For example, if you want to reference the last argument of the previous command, you would type:

echo !$

This will print the last argument of the previous command to the console.

Conclusion

Bash history

Комментарии

Популярные сообщения из этого блога

How To Modify CSS Classes in JavaScript

How To Backup MySQL Databases on an Ubuntu VPS

How To Backup PostgreSQL Databases on an Ubuntu VPS