bash – its not a bug its a feature

It’s not a bug it’s a feature

It has been said so many times it goes beyond cliche.  It is really easy to try and explain away odd behaviors as some sort of esoteric feature

It took a while before I caught up on the pattern.  I have a habit of copying text between windows, usually in the form of a command.  Putty makes this really easy, simply highlight the text in one window and right click in another.

The reason I did the copy and paste in the first place was because the command was really long.  Imagine my surprise when I try and run it a second time only to discover it is not in my command history.

If you start your command with one or more spaces, then it will usually fail to show up in the command history.  It is the variable HISTCONTROL that defines if command begins with a space it should be omitted.  This is done when the value is defined as ignorespace. There are two other possible values for the HISTCONTROL variable, they are ignoredups and ignoreboth – the ignoredups does exactly what it says.  If you do the exact same command multiple times the duplicates will be suppressed.  The ignoreboth option simply combines both ignoredups and ignorespace.

The command history is stored in the file that is pointed to by the HISTFILE variable.

HISTFILE=/home/dock/.bash_history

The history file isn’t without limit, the number of commands that will be stored is defined by the HISTFILESIZE variable.

HISTFILESIZE=2000

It is possible to see the last few commands in a few ways.  If the command is really recent, simply use the up and down arrow to scroll through the commands.  When you find the one you are interested it is already typed, just press enter.  To list the last commands simply type history.

 973 ls
 974 ls -ltr
 975 sync
 976 cd /
 977 sync
 978 sudo init 0
 979 echo $HISTIGNORE
 980 ls
 981 histroy
 982 history

The history command will display the last HISTSIZE commands

HISTSIZE=100

This will show the last 100 or even last 1000 commands.  This is probably the easiest way to see the commands, but it is also possible to see the last few commands using the fc command (ie fc -l).

Starting a command with a space prevents it from showing up in the list of the previous commands, but it is possible to extend the filtering to other commands.  There is another variable HISTIGNORE which can be set to include additional commands.  The variable is just a colon delimited list of commands.

HISTIGNORE='ssh:fortune:compress'

This example will suppress any commands that begin with ssh, fortune or compress.  This in itself may be enough, to keep those really embarrassing commands that contain passwords out of the history file.  It is possible to not just supress these commands but any line that contains one of those commands.

HISTIGNORE='*ssh*:fortune:compress'

Simply add the asterisk and then ssh will no longer be added to the command history. This is a shell pattern not a regular expression.  Only shell patterns will be used, not regular expressions.  I don’t have any specific filters that I need, simply adding an asterisk to my command.

HISTIGNORE="[0-9]*"

This filter works but it doesn’t do much that is meaningful.

It is possible to remove any of those embarassing commands by deleting them.  Simply get a list of the commands and then delete the one based on the index number.

> history
 1031 ls
 1032 history
 1033 pwd
 1034 cwd
 1035 cd
 1036 history
>
> history -d 1034
> history
 1031 ls
 1032 history
 1033 pwd
 1034 cd
 1035 history
>

Actually it is a feature, not a bug.

This entry was posted in Command line. Bookmark the permalink.