You probably know about using the up and down arrow keys to scroll through your Bash history, but did you know that there's a lot more to Bash history than just repeating commands? There is much more to the story. Or, should that be much more to the history? In either case, the history command is one of those obscure commands that is powerful and handy to know on at least a basic level. This article will take the mystery out of Bash history to make it a more friendly sysadmin tool.
The background
In Bash, your command history is stored in a file (.bash_history
) in your home directory. The leading (.) makes the file hidden from normal view. To see it, issue the ls -a
command.
$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc
You can operate on the .bash_history file as you would any other plain ASCII text file.
The three related environment variables you need to be aware of are HISTFILE, HISTFILESIZE, and HISTSIZE.
- HISTFILE—/home/<username>/.bash_history
- HISTFILESIZE—1000
- HISTSIZE—1000
The HISTFILE variable holds the name and location of your Bash history file. HISTFILESIZE is how many commands can be stored in the .bash_history
file. HISTSIZE is the number of cached commands. Once you reach 1000 commands, the oldest commands will be discarded as new ones are saved.
$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
1000
$ echo $HISTFILE
/home/khess/.bash_history
Each user, including root, has these variables and sizes assigned by default. If you want to change these variables, edit the .bashrc file in your home directory. For example, if you want to change the size variables to 500 commands, edit the .bashrc
file and use the following entries:
export HISTSIZE=500
export HISTFILESIZE=500
To use the new values without logging off and back on again, you can execute the .bashrc
file.
$ . ~/.bashrc
Your values are now active for your current shell and any subshells.
$ echo $HISTSIZE
500
$ echo $HISTFILESIZE
500
If you don't want your history to keep duplicated commands, you can instruct your history to ignore duplicate entries by adding the following to your .bashrc file:
export HISTCONTROL=ignoredups
The problem
The problem with Bash history is that it's not written to the .bash_history
file until you log off. This makes it impossible to use the history command for scripting. For example, say you're teaching a Linux class, and you want to check to see if students have run a particular command to copy their files to a mounted external drive. If the student stays logged onto the system, you won't be able to determine if the copy has been performed because your script uses their history file (/home/student/.bash_history) to check if the command has run.
The solution
To work around this feature, use the write option for the history
command. As part of the class instruction, you'd have the students run this command to save their history even if they don't log off.
$ history -w
This writes all current session command history to the HISTFILE. And now your check script can easily find if the command has been run. You also have to trust that the student will run the history command.
[ Check out Seth Kenlon's history lesson: Make Bash history more useful with these tips ]
Another problem
Since the HISTSIZE and HISTFILESIZE are relatively large (1000 commands), it's possible that the student ran the command before. You can add the export HISTCONTROL=ignoredups entry into their .bashrc, but this won't help if the student ran a similar command two days ago because the file will only show a single command.
The solution to this problem
To work around this problem, you should clear the student's history upon logging on. Edit the .bashrc file again and add the following command to it.
history -c
This command clears their history on each log on and removes the problem of picking up a previously run command. You don't have to worry about picking up a command from previous sessions because the system logs off idle users after a specified period of time if you set the idle time limit in /etc/profile
. Edit the /etc/profile
file and add the following entry.
TMOUT=300
The TMOUT variable uses a number of seconds of idle time. In this case, 300 seconds is five minutes. You can set this value to any number of seconds. This value will take effect on the next log on for all users. To set individual timeout values, use .bashrc
but realize that users may change this file at will.
[ Download now: A sysadmin's guide to Bash scripting. ]
Wrap up
Bash history is a handy tool, but it can be frustrating if you aren't familiar with its options and quirks. There are many more options available in history. Check the man pages for further information. And remember that each command you enter is history in the making.
About the author
Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization.
Follow him on Twitter: @kenhess for a continuous feed of Sysadmin topics, film, and random rants.
In the evening after Ken replaces his red hat with his foil hat, he writes and makes films with varying degrees of success and acceptance. He is an award-winning filmmaker who constantly tries to convince everyone of his Renaissance Man status, also with varying degrees of success and acceptance.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit