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.
Sobre el autor
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.
Navegar por canal
Automatización
Las últimas novedades en la automatización de la TI para los equipos, la tecnología y los entornos
Inteligencia artificial
Descubra las actualizaciones en las plataformas que permiten a los clientes ejecutar cargas de trabajo de inteligecia artificial en cualquier lugar
Nube híbrida abierta
Vea como construimos un futuro flexible con la nube híbrida
Seguridad
Vea las últimas novedades sobre cómo reducimos los riesgos en entornos y tecnologías
Edge computing
Conozca las actualizaciones en las plataformas que simplifican las operaciones en el edge
Infraestructura
Vea las últimas novedades sobre la plataforma Linux empresarial líder en el mundo
Aplicaciones
Conozca nuestras soluciones para abordar los desafíos más complejos de las aplicaciones
Programas originales
Vea historias divertidas de creadores y líderes en tecnología empresarial
Productos
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Servicios de nube
- Ver todos los productos
Herramientas
- Training y Certificación
- Mi cuenta
- Soporte al cliente
- Recursos para desarrolladores
- Busque un partner
- Red Hat Ecosystem Catalog
- Calculador de valor Red Hat
- Documentación
Realice pruebas, compras y ventas
Comunicarse
- Comuníquese con la oficina de ventas
- Comuníquese con el servicio al cliente
- Comuníquese con Red Hat Training
- Redes sociales
Acerca de Red Hat
Somos el proveedor líder a nivel mundial de soluciones empresariales de código abierto, incluyendo Linux, cloud, contenedores y Kubernetes. Ofrecemos soluciones reforzadas, las cuales permiten que las empresas trabajen en distintas plataformas y entornos con facilidad, desde el centro de datos principal hasta el extremo de la red.
Seleccionar idioma
Red Hat legal and privacy links
- Acerca de Red Hat
- Oportunidades de empleo
- Eventos
- Sedes
- Póngase en contacto con Red Hat
- Blog de Red Hat
- Diversidad, igualdad e inclusión
- Cool Stuff Store
- Red Hat Summit