TL;DR: If your history begins to truncate (e.g., your HISTSIZE is 400 but your history number is 500), resetting your history with history -c will clear HISTSIZE or fewer lines from your history, and subtract that many from your history number, but will not reset it to 0 (in the example, it would reset it to 100). How can I completely reset the history number back to 0?

Bash 4.2 (and also on 4.4). I want my history numbers to be under 4 digits. relevant bashrc sections:

#ignores back-to-back duplicate commands, not those that start with a space
#ignore commands fewer than 5 characters long because we can type those

the history number as seen with history or \! in the PS1 will obviously go beyond these values. I want to be able to reset the history number down to ~100 when it goes above 1,000.

history -c clears the terminal history, but that only reduces the history number by the amount of commands that it cleared, e.g.:

j:~$ history | wc -l
j:~$ echo $HISTCMD 
j:~$ history -c
j:~$ echo $HISTCMD

So... how do I drop the history number down to a smaller number, such as $HIST(FILE)SIZE. This is in a tmux session, but I don't think that should matter.

Aside: is there a method to get the current history number as a variable that requires fewer pipes than the one I'm currently using? EDIT: as Jeff mentioned, $HISTCMD stores this value.

None of the solutions provided work. Here's what they do with set -x:

history -c doesn't clear history

  • 1
    along the way, there's $HISTCMD, unless it's been unset. – Jeff Schaller Aug 16 '18 at 17:02
  • I think a large part of your question has already been answered – Jeff Schaller Aug 16 '18 at 17:06
  • @JeffSchaller, the command number, AFAIK, is different than the history number. one is \# and one is \!. One changes with history -c and one doesn't – jeremysprofile Aug 16 '18 at 21:55
  • There is a solution on here. it seems that four steps are needed. Read the description for the reason of HISTFILESIZE=$HISTSIZE. – Isaac Aug 26 '18 at 22:33
  • @Isaac, I added a picture to show that solution failing. – jeremysprofile Aug 27 '18 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.