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.

CentOS 7

Show Display Date And Time For Command History

Share on LinkedIn

How would you display shell command history with date and time under Linux or UNIX operating systems? For instance, CentOS?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows:



echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

To see history type


Sample Output:

1002 29/01/15 11:11:07 hwclock --systohc
1003 29/01/15 11:11:10 date
1004 29/01/15 11:11:22 hwclock -show
1005 29/01/15 11:11:25 hwclock -r
1006 29/01/15 11:11:32 history
1007 29/01/15 11:21:15 ip addr
1008 29/01/15 11:21:19 show ip addr
1009 29/01/15 11:21:28 history | grep ip
1010 29/01/15 11:22:34 ip addr sh
1011 29/01/15 11:22:42 hostname
1012 29/01/15 11:22:49 ifconfig
1013 29/01/15 11:24:01 history

Note: Date and Time will only appear for new commands. All previous commands will be stamped with the time and date you issued the ‘HISTTIMEFORMAT’ command.