How To Make Writing to .bash_history Immediate

In Red Hat Linux, this is accomplished by adding a small script to the system’s profile.d directory. It is possible to edit existing files like bashrc but you may lose those changes when upgrading. I usually create a custom profile script that contains this as well as other global variables and place it at /etc/profile.d. For this post, we’ll create and get this setup.

It’s best if you login or su as root before you begin.

Create the file for the script

vim /etc/profile.d/

Add this to the script


# Location of this script: /etc/profile.d/

# Purpose of script is to makes writing to .bash_history immediate
shopt -s histappend

# End of script

Save the script and exit by typing


and press Enter. This writes and quits out of the vim editor.

Set permissions and ownership of file

chown root:root /etc/profile.d/
chmod 644 /etc/profile.d/

Make the script executable

chmod +x /etc/profile.d/

Log completely out, log back in, run a couple of commands (any commands like history, ls, etc.), and then check the history by running:

cat .bash_history

You should see the last couple of commands you just ran.


A quick break-down of the script itself

The first command changes the .bash_history file mode to append. And the second configures the ‘history -a’ command to be run at the shell. The ‘-a’ immediately writes the current/new lines to the .bash_history file.

Posted in Bash Shell, Linux SysAdmin, Scripting, Scripts and tagged , , , , , , , , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *