3

Problem

Thanks to this question, I succeed to forbid my bash to stock the same command more than one time if I write many times the same command.

However, there is still a detail that I cannot resolve: my prompt contains the \# special character that print command number for each command. And it still increment itself each time I enter a command, duplicate or not, even if my history doesn't fill up.

Sample

In my ~/.bashrc, my prompt looks like this:

export PS1='aracthor \# >'

And when I write many times the same command, the command number increment:

aracthor 1 >pwd
/home/aracthor
aracthor 2 >pwd
/home/aracthor
aracthor 3 >

How can I avoid command number it to increment ?

I'm using Ubuntu 14.04.02 and declare -p HISTCONTROL output is:

declare -x HISTCONTROL="ignoredups:erasedups"
  • Add Ubuntu version and output of declare -p HISTCONTROL. – Cyrus Jun 23 '15 at 16:38
  • Also, could you explain why you would want this? I'm asking because this sounds like an XY problem, there might be a better way to achieve what you're after. – terdon Jun 23 '15 at 16:47
  • @Cyrus I added my Ubuntu version and my actual HISTCONTROL. @terdon There is not really an useful goal or a goal at all, I just don't want to see my prompt number incremented on duplicate. – Aracthor Jun 24 '15 at 6:57
1

Normally \! will increment as you want, but when you reach the history limit HISTSIZE of lines to keep in memory it will stop. You could try a much larger HISTSIZE.

Or instead, you could set a larger HISTSIZE and HISTFILESIZE and run a command before each prompt to save your history:

PROMPT_COMMAND='history -w'

Then in your PS1 you can run a command to count the lines of history in the file:

PS1='\w #=\# !=\! file=$(wc -l <$HISTFILE) $ '
  • I am not looking for the \! character, It does not increment on duplicates, as I want. But the \#, that count commands from the last bash start, increment on duplicates, and I want to avoid that. – Aracthor Jun 26 '15 at 12:25
1

Read man bash, the set builtin, and the section on HISTCONTROL:

       HISTCONTROL
          A colon-separated list of values controlling how commands are saved on the history list.  If the list of values includes ignorespace, lines
          which begin with a space character are not saved in the history list.  A value of ignoredups causes lines  matching  the  previous  history
          entry  to not be saved.  A value of ignoreboth is shorthand for ignorespace and ignoredups.  A value of erasedups causes all previous lines
          matching the current line to be removed from the history list before that line is saved.  Any value not in the above list is  ignored.   If
          HISTCONTROL  is  unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the
          value of HISTIGNORE.  The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regard‐
          less of the value of HISTCONTROL.

In my ~/.bashrc I have:

export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups

Here is an example:

w3@aardvark:~(0)$ echo $HISTCONTROL
ignoreboth
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:56 EDT 2015
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:57 EDT 2015
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:58 EDT 2015
w3@aardvark:~(0)$ history
    1  cd
    2  echo $HISTCONTROL
    3  date
    4  history
w3@aardvark:~(0)$  

Executions of date after history will be logged, and counted.

  • One time again, I'm NOT looking for history log, what I have already works. I am only looking for a way to stop command line count to increment. I added a sample. – Aracthor Jun 28 '15 at 9:37
  • I am stumped. If you are dedicated, you could sudo apt-get install apt-src and apt-src install bash, then look at the source. – waltinator Jun 28 '15 at 18:52

Your Answer

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

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