10

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?

12

You can reverse the default Ubuntu settings by setting two variables (HISTIGNORE an HISTCONTROL), use the commands like below:

HISTIGNORE='!( *)'
HISTCONTROL=ignoredups

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 (with HISTCONTROL= ) 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 with extglob turned on, with shopt -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.

  • thanks, that's exactly what I wanted.. saved the changes to .bashrc.. I needed that to prevent bash from recording every cds, lss and other everyday commands that I use; and for those few commands that I need in history, I can now use the prefix.. – precise Feb 21 '14 at 4:05
  • 1
    @rusty If you only want to exclude some trivial commands, it might be better for you if you use something like HISTIGNORE='ls*:cd*' , that will make history ignore every line starting with ls or cd, you can extend that list with other commands, just put a : between the commands. But don't forget that the command recall feature which you can access by the "up arrow key" is also uses this history, so if you set something to be ignored, you won't be able to recall those ignored commands with the "up arrow". – falconer Feb 21 '14 at 7:15
  • I wanted to ignore any SSH command, but not commands like ssh-copy-id or ssh-keygen. In HISTIGNORE, I included "ssh\ *" with my other ignored commands. Escaping the space char appears to be mandatory. – user38537 May 16 '17 at 4:03
2

"The commands executed in the terminal prefixed with blank space(s) are not recorded in the command history file"

Yes they are.

terminal history

  • 1
    it is a setting in bash ;-) – Rinzwind Feb 20 '14 at 15:32
  • command prefixed with blank spaces.Like ` sudo apt-get update` – Avinash Raj Feb 20 '14 at 15:34
  • What's the value of your HISTCONTROL variable? – glenn jackman Feb 20 '14 at 15:37
  • @AvinashRaj : yeah, see me command "this shouln't be recorded", I put a space (visible both in the command line and the history). So it doesn't work that way. Maybe you can configure it (with HISTCONTROL); but it isn't the case by default. – MrVaykadji Feb 20 '14 at 18:31

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.