In Linux, shell history refers to a feature that keeps track of the commands executed in a shell session. It allows users to view and reuse previously executed commands, providing convenience and efficiency.
The shell history feature records the commands entered by a user, along with the corresponding timestamps. This history is typically stored in a file, often located in the user’s home directory, such as ‘~/.bash_history’ for Bash or ‘~/.zsh_history’ for Zsh.
Some key aspects of shell history in Linux include:
- Command Recall: Users can easily recall and execute previously entered commands by using keyboard shortcuts or specific commands such as history.
- Command Line Editing: Shell history enables command line editing, allowing users to navigate through their command history, edit commands, and reuse or modify them.
- Command Expansion: Users can utilize command expansion techniques, such as using exclamation marks (!), to reference and execute specific commands from their history based on patterns or indices.
- Configuration and Customization: Users can adjust the behavior of the shell history feature through configuration files like .bashrc or .zshrc. These files allow customization options such as controlling the size of the history file, ignoring specific commands, or adjusting the format of timestamps.
Top shell history commands:
- repeating the last command
- repeating other commands
- prevent recording a command
Now, we will discuss on the above commands in details
In Linux, you can repeat the last executed command using the “!!” or “!-1” symbol (called as bang bang). These methods allow us to quickly re-execute the most recent used command without typing it. Typing “bang bang” symbol and entering the Enter key in terminal will execute the last command.
Here is an example of repeating the last command using “bang bang”:
In Linux, you can repeat specific commands from the command history using their history number with “!<number>”, partial match with “!<partial_command>” searching history with “history | grep”, or using Ctrl+R for reverse history search.
Here is an example of repeating other commands:
In Linux, the “history” command is used to display a list of previously executed commands in the current shell session. It provides a history of the commands entered by the user, along with their corresponding line numbers. The “history” command is a built-in command in most Unix-like operating systems, including Linux.
Here are some key features and usage examples of the history command:
Displaying Command History: Running the “history” command without any options will display a list of previously executed commands in the current session. Each command is listed with its line number.
Here is an example of “history” command:
Limiting the Number of Commands Shown: By default, the “history” command displays a certain number of recent commands. You can specify the maximum number of commands to be shown using the “-n” option.
In the below example, we can see history of last 5 commands we used:
Clearing Command History: To clear the “command history” for the current session, you can use the “-c” option. This will remove all the recorded commands from the history.
Searching Command History: The output of the “history” command can be piped to other commands, such as grep, to search for specific commands based on keywords or patterns.
Here is an example of “searching command history” in Linux:
In the above example, we listed the command history of “mkdir”.
In Linux, the “!n” command, where “n” is a number, is used to execute a specific command from the command history based on its line number. It allows you to quickly repeat or recall a command without having to type it out again.
Here is an example of “!n” command:
In Linux, the “Ctrl+R” command is a keyboard shortcut that initiates a reverse history search in the shell. It allows you to search and recall previously executed commands based on matching patterns.
Here’s how to use “Ctrl+R” for reverse history search:
1. Press “Ctrl+R” on your keyboard. This will activate the reverse history search mode in the shell.
2. Start typing a portion of the command you want to search for. As you type, the shell will search through your command history for commands that match the entered text.
3. Continue typing or press “Ctrl+R” again to find the next match. If multiple commands match the entered text, you can press “Ctrl+R” again to cycle through them.
4. Press Enter to execute the selected command. Once you’ve located the desired command using “Ctrl+R”, you can press Enter to execute it.
In the above example, we can see as soon as I pressed “Ctrl+R” key, search mode got enabled and when I pressed “t” key, all possible results are reflecting.
In Linux, the “$HISTSIZE” command is an environment variable that determines the maximum number of commands that are stored in the command history. It specifies the size or length of the command history list.
By default, the value of “$HISTSIZE” is often set to a predefined number, such as 1000, but it can be customized to suit your preferences. The command history is stored in memory and typically associated with your user account.
Here’s how you can work with the “$HISTSIZE” command:
To modify the default value:
This example will change the default history value to 1200.
In Linux, the “$HISTFILE” environment variable refers to the path and filename of the file where the shell history is stored. The shell history typically contains a record of the commands that have been executed in a shell session.
Whenever you execute a command in a shell, it is usually saved to the history file. This allows you to easily recall and reuse previously executed commands by accessing the command history. The “$HISTFILE” variable specifies the location where this history is stored.
The default value of “$HISTFILE” varies depending on the shell you are using. In many Linux distributions, the default value is usually set to “~/.bash_history” for the Bash shell. This means that the history file is located in the user’s home directory with the filename “.bash_history”. Other shells may use different default filenames or locations.
You can customize the value of “$HISTFILE” to specify a different file or location for your shell history. For example, you can set it to “~/myhistory.txt” to save your history to a file named “myhistory.txt” in your home directory.
In Linux, the “$HISTFILESIZE” environment variable determines the maximum number of lines that can be stored in the shell history file specified by “$HISTFILE”. It sets a limit on the size of the history file, preventing it from growing indefinitely and consuming excessive disk space.
When a command is entered in a shell session, it is appended to the history file. If the number of lines in the history file exceeds the value of “$HISTFILESIZE”, the oldest commands are removed from the file to accommodate the new command.
By default, the value of “$HISTFILESIZE” is often set to a relatively large number, such as 500 or 1000, depending on the Linux distribution and shell configuration. However, you can modify this value to suit your preferences.
To set a specific value for “$HISTFILESIZE”, you can add the following line to your shell’s configuration file (e.g., “~/.bashrc” for Bash):
Here is an example of this command:
In this example, the history file will be limited to a maximum of 1200 lines.
Keep in mind that “$HISTFILESIZE” controls the number of lines in the history file, while the number of commands retained in the active history for the current session is controlled by the “$HISTSIZE” environment variable. Both variables work together to manage the command history in a shell.
To prevent a specific command from being recorded in the command history in Linux, you can use a “space” prefix.
Here is an example of prevent recording a command:
In the above example, the last three commands are “mkdir dir30”, “mkdir dir40” and “mkdir50” but while checking history it is not showing “mkdir dir50” because while executing this command, we added “space” as a prefix.
Wrapping up shell history
So, in this section we discussed about different “shell history” commands. In Linux, the shell history feature records previously executed commands, allowing users to view, recall, and reuse them. The history command displays a list of commands with their line numbers. Shell history enhances productivity by providing quick access to past commands and facilitating command line efficiency. If you wish to learn more on this topic, please follow here or wish to learn shell variables, please do follow here.