The commands executed in the terminal prefixed with blank space(s) are not recorded in the command history file. But is there a way to get the reverse i.e. the history should only record those which are prefixed with space?
Ubuntu – Get bash history to remember only the commands run with space prefixed!
bashhistory
Related Question
- Ubuntu – How to uniqely record every new command I use, and possibly timestamp it
- Ubuntu – centralized history file for all the commands run from various terminals
- Ubuntu – How to save terminal history manually
- Ubuntu – Select commands from bash history
- Ubuntu – How to delete selected results from bash history
- Ubuntu – get the commands from history executed in bash in the last 15 mins
Best Answer
You can reverse the default Ubuntu settings by setting two variables (HISTIGNORE an HISTCONTROL), use the commands like below:
HISTCONTROL=ignoredups
changes history behaviour to only ignore duplicate lines, and not ignore lines starting with a space. (You can also set HISTCONTROL to the empty string (withHISTCONTROL=
) if you do want to keep duplicates, but that is usually not wanted.)HISTIGNORE='!( *)'
makes history ignore every line which doesn't start with a space:( *)
would match every line starting with a space, but the leading!
negates the match, so it matches everything which doesn't start with a space. And everything what is matched by the HISTIGNORE pattern will be ignored by history. (This latter option requires that bash is run withextglob
turned on, withshopt -s extglob
, but that is the default setting on Ubuntu.)If you want to make this permanent, don't forget to put the above two commands into your
~/.bashrc
.