Using the "history" Command (with examples)

Using the "history" Command (with examples)

The “history” command is a built-in command in the Bash shell that allows users to view and manipulate the command-line history. This command is particularly useful for recalling and reusing previously executed commands, as well as managing the command history.

Display the commands history list with line numbers

history

Motivation: This use case is helpful when you want to quickly view the list of previously executed commands and their corresponding line numbers. It allows you to easily identify a specific command from the history for future use.

Explanation: Running the “history” command without any arguments will display the command history list with line numbers. Each line in the output represents a command that was executed in the current session, and the number at the beginning of each line indicates the line number of the command.

Example output:

1  ls
2  cd Documents/
3  git status
4  vi README.md
5  history

Display the last 20 commands

history 20

Motivation: If you want to view a specific number of the most recent commands from the command history, you can use the “-n” option followed by the desired number. This is useful when you only need to revisit the recent commands and don’t want to overwhelm yourself with the entire history list.

Explanation: The “history” command accepts a numerical argument, which specifies the number of commands to be displayed. In this example, we pass the argument “20” to display the last 20 commands executed in the current session.

Example output:

503  ls
504  cd Documents/
505  git status
506  vi README.md
507  history
508  ls
509  cd Downloads/
510  git commit -m "Update README.md"
511  git push origin main
512  cd ../
513  ls
514  man history
515  history 20
516  clear
517  cd Projects/
518  mkdir my_project
519  cd my_project/
520  code .

Clear the commands history list

history -c

Motivation: Resetting the command history can be useful in situations where you want to clear any sensitive information that might be stored in the history. This ensures that the next person using the shell or reviewing the history won’t be able to access previous commands.

Explanation: The “-c” option is used to clear the entire command history list. When you run this command, all the entries in the history will be deleted, leaving an empty history list.

Example output: N/A (the command clears the history, so there is no output)

Overwrite history file with history of current shell

history -w

Motivation: Saving the command history to a file can be helpful for future reference or for sharing the history with other users. The “-w” option allows you to overwrite the contents of the history file with the current shell’s history.

Explanation: When you execute the “history -w” command, the command history is written to the history file specified by the value of the HISTFILE variable. The previous contents of the history file are replaced with the history of the current shell.

Example output: N/A (the command saves the history to a file, so there is no output)

Delete a specific history entry

history -d offset

Motivation: There might be instances where you want to remove a specific command from the command history. This allows you to selectively delete entries that are no longer relevant or contain sensitive information.

Explanation: The “-d” option, followed by an offset value, is used to delete a specific history entry. The offset represents the line number of the command in the history list.

Example command: To delete the command at line number 5 in the history, you would use:

history -d 5

Example output: N/A (the command deletes the specified history entry, so there is no output)

By utilizing the different use cases of the “history” command, you can effectively manage your command history and easily recall and manipulate previously executed commands.

Related Posts

How to use the command "cat" (with examples)

How to use the command "cat" (with examples)

The “cat” command in Unix is used to print and concatenate files.

Read More
Git delete-submodule (with examples)

Git delete-submodule (with examples)

The git delete-submodule command is a powerful tool that allows you to delete a specific submodule from a Git repository.

Read More
Managing Git Packaging Repositories with pkgctl repo (with examples)

Managing Git Packaging Repositories with pkgctl repo (with examples)

Introduction Git is a popular version control system that allows developers to track changes in their code and collaborate with others.

Read More