The bash history feature can save you a lot of time when repeating commands or examining commands you’ve entered previously. Credit: Gorodenkoff / Shutterstock Command history on Linux allows you to review and repeat previous commands with little effort. It is built into the bash shell (included in the shell executable). It’s a helpful feature if you want to rerun commands without retyping them or examine recently run commands when you’re trying to determine when or how some recent change might have been made. If you type “which history” on the command line, you can expect to see a response that indicates the shell can’t find it. Of course, that’s only because the which command is looking for an executable to run – not for a shell built-in. This post takes a look at how the history command works and how to best use it. (See also: Bash command cheat sheet) For starters, it’s helpful to understand that bash saves commands that you enter into a command history file called .bash_history. One of the important things to know about this file, however, is that you can type a command like the one below and then find that it’s not sitting at the bottom of your .bash_history file – at least, not yet: $ echo Can you find me?Can you find me?$ tail -2 .bash_historycdcat myfile At the same time, if you ask the history command to display recently entered commands, the command will be included in the output. $ history | tail -3 1028 echo Can you find me? 1029 tail -2 .bash_history 1030 history | tail -3 This behavior is because bash stores commands in a buffer and only adds them to the history file when you log off or exit the shell. On your next login, the contents from your previous session will have been added. How many commands are saved? You can control the number of commands that will be saved in your history file. The default is usually 1000. To confirm the length of your command history, type the command shown below. $ echo $HISTSIZE1000 To change this to a different size, add a line to the bottom of your .bashrc with a command like those shown below that change the number of commands to be saved to 2,000. The 4th line sources the .bashrc file to adopt the change. $ echo $HISTSIZE1000$ echo "HISTSIZE=2000" >> ~/.bashrc$ . .bashrc$ echo $HISTSIZE2000 You can change the size if you find you’re saving too many or too few commands. To change the default setting for $HISTSIZE, use root privilege and change the setting in /etc/profile. Note, however, that this will not change the setting for existing accounts. Avoid saving duplicate commands To avoid saving a command multiple times when entered some number of times in a row, use the HISTCONTROL option to ignore duplicates. To check your current settings, use these commands: $ echo $HISTFILE/home/justme/.bash_history$ echo $HISTSIZE1000$ echo $HISTCONTROLignoredups The settings shown display the name and location of the history file, the number of commands to be saved and that the ignoredups setting is already set up. If you type the whoami command three times in a row and then run the command shown below, you will only see the whoami command listed once. $ history | tail -4 1049 echo $HISTFILE 1050 echo $HISTCONTROL 1051 whoami 1052 history | tail -4 Using your command history To rerun a particular command that’s been saved in your history file, type an exclamation point followed by the line number from the file. Here’s an example: $ !15echo $HISTSIZE1000 As shown above, the command will be displayed and then run. You can also scroll through prior commands by pressing on the up arrow on your keyboard. This will move from the most recent command to the prior one, etc. Once you get to the one that you want to run, simply press the Enter key. Note that you can make changes to the command before you press the enter key. Another way to rerun a command is to type an exclamation point followed by the first letter(s) of the command that you want to rerun. This will choose the most recent command that matches the letters you type. Here’s a simple example: $ echo 11$ echo 22$ echo 33$ !eecho 33 Ignoring commands not worthy of remembering Command history can prove a very handy tool for rerunning commands – especially complex ones – without having to retype them. At the same time, many commands that you use often will prove not worthy of being remembered. The command below will ensure that the four commands included will *not* be remembered. $ export HISTIGNORE='pwd:date:history:clear' Once you use a command such as this, your command history will no longer include these commands. Here’s an example: $ export HISTIGNORE='pwd:date:history:clear'$ dateThu Sep 26 03:57:08 PM EDT 2024$ pwd/home/newuser$ history | tail -2 41 export HISTIGNORE='pwd:date:history:clear' 42 history | tail -3 Note that the commands included in the HISTIGNORE setting are no longer being saved. $ history | tail -2 44 date 45 history | tail -2$ export HISTIGNORE='pwd:date:history:clear'$ dateThu Sep 26 04:02:30 PM EDT 2024$ pwd/home/newuser$ history | tail -2 46 export HISTIGNORE='pwd:date:history:clear' 47 history | tail -2 The date and pwd commands were run, but not added to the history command buffer once the HISTIGNORE setting was in place. Add the HISTIGNORE settings to your .bashrc file if you want it to remain active when you log in again. Note that, given the settings shown above, use of the history command will not be added to your command history file. However, commands like “history | tail -10” will be saved. If you want to avoid this, add “history *” to your settings like this: HISTIGNORE='pwd:date:history:history *:clear' Wrap-up The Linux history command can prove a very useful and time-saving tool. Controlling how many commands are saved and restricting which commands are saved may make reusing commands quite a bit more efficient. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe