Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 2221  / 2 Years ago, tue, october 11, 2022, 1:59:59

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?


More From » bash

 Answers
2

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.


[#26896] Tuesday, October 11, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kroapathe

Total Points: 445
Total Questions: 117
Total Answers: 99

Location: Botswana
Member since Sun, Sep 19, 2021
3 Years ago
;