10

I'd like to add the date/time to the output of the history command in CentOS/RHEL 6. I would like this to apply to all users on the machine by default. This itself is easy. It seems to just be a case of adding:

export HISTTIMEFORMAT="%Y%m%d %T  "

Where should that be added? I've seen some conflicting info on whether to add this to /etc/profile or /etc/bashrc. I believe both would work equally well.

Does anyone have any advice as to which file I should use for this specific modification?

batfastad
  • 1,035

2 Answers2

4

I'd create a file with that string in it and add it to this directory, /etc/profiled.d. Call the file custom_histformat.sh, or something similar.

Generally you don't want to add customizations to either of those 2 files (/etc/profile or /etc/bashrc), since they're owned by the system. Customizations can go into the /etc/profile.d directory.

UPDATE #1

At the risk of beating a dead horse I thought it important to highlight why one would use /etc/profile.d over the directories that were mentioned in the question or in @Gilles answer.

If you look through any of the testing around RHCE they'll specify that /etc/profile.d is an appropriate place to make this type of change. So if you're taking the test and answer anything other than this, or /etc/bashrc or /etc/profile, you'd be wrong. Call it a RH-ism but this is just how it's done on distros based on Red Hat.. Other distros have other approaches.

slm
  • 369,824
  • This is great info. I was not aware of profile.d and what you say makes perfect sense. I also was not sure as to whether this would be something specific to bash that would be best placed in a bash-specific file or something that can apply to all shells. Thanks! – batfastad Nov 06 '13 at 16:52
  • 1
    This applies to debian, too. (/etc/profile.d/ works out of the box.) – VasyaNovikov Aug 28 '15 at 12:41
4

If you define an environment variable for all users, it belongs in /etc/profile or /etc/environment. If you define an environment variable for your account, it belongs in ~/.environment or ~/.profile.

HISTTIMEFORMAT works if you set it in the environment, but you shouldn't do it this way, because different shells may interpret it differently. (I don't think this is an issue for this particular variable however.) You should instead set it in bash's initialization file: ~/.bashrc. Some installations have a system-wide bash initialization file like /etc/bashrc, but since this is a user preference, you shouldn't use that.

So put HISTTIMEFORMAT="%Y%m%d %T " in ~/.bashrc.

For the differences between these files, see Is there a ".bashrc" equivalent file read by all shells?

  • 1
    He wants to do it for all users, so /etc/profile.d is the appropriate place on a RH box. If you look in this directory on any RH based distro it's littered with example after example of exactly what he's trying to do. – slm Oct 29 '13 at 00:05