Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On CentOS 6.2 and Bash 4.1.2(1), I have added a file in /etc/profile.d which contains:

HISTTIMEFORMAT='%c : '
history -s "# some text as a marker"

That works insofar as the the variable gets set and the marker is written to the user's history list (and to ~/.bash_history when they exit).

The problems are:

  • history -s doesn't add a timestamp when it adds the marker
  • scrolling back through history stops at the marker (the ~/.bash_history file isn't read into memory)
  • the history entry for the marker comment has "??" instead of the (missing) timestamp

I tried adding:

history -s "#$(date +%s)"

before the other history -s command, but Bash sees this as a command in history rather than a timestamp even though it's in the same format.

Another option is to append the marker and a timestamp directly to ~/.bash_history, but then they are not contiguous with the activity in the session's history when it later gets appended. There may be other sessions that get appended between the marker and its related session due to other users or multiple sessions by a single user.

Is there any way to solve these problems?

Update:

I have tried adding set -o history (and removed the history -s "#$(date +%s)" line) and it seems to solve the history scrolling problem mentioned above, but the lack of timestamp is still a problem.

share|improve this question
    
eval history -s "#$(date +%s)" ?? Good luck. –  shellter Jun 1 '12 at 23:42
add comment

1 Answer

up vote 1 down vote accepted

I posted this question on the help-bash mailing list and got a response from Chet Ramey, the maintainer of Bash.

He suggested adding either of the following before the history -s command:

set -H    # aka set -o histexpand

or

histchars='!^#'

I tried each of them and they both work. The reason is that the history comment character which defaults to # hasn't been set at the point that the profile code is running and so is null (\0).

Here's my working file:

HISTTIMEFORMAT='%c : '
histchars='!^#'
set -o history
history -s "# some text as a marker"
share|improve this answer
    
Saw your mail first. Think I might do that, (add a marker)... it's a good idea. –  ormaaj Jun 23 '12 at 20:57
    
This did not work for me (Bash 4.1.5), neither -H, histchars nor set -o history. To the contrary, all lines of my .bashrc are added that are followed by set -o history even though history is on by default. I consider this a bug. However, I used it for a workaround: I put histchars='!^#', followed by set -o history and history -s "# some text as a marker" at the very end of .bashrc. This adds indeed the marker and does not delete the history. –  xebeche Nov 3 '13 at 22:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.