How to manage and disable bash history

You can change Bash history behaviour changing environment variables. Here is simple reference about this settings.

HISTCONTROL

code-block:: bash

export HISTCONTROL=ignorespace

Lines which begin with a space character are not entered on the history list.

code-block:: bash

$ export HISTCONTROL=ignorespace $ echo „nospace” nospace $ echo „space” space $ history 2 export HISTCONTROL=ignorespace 3 echo „nospace” 4 history

code-block:: bash

HISTCONTROL=ignoredups

Lines matching the last history line are not entered.

code-block:: bash

$ export HISTCONTROL=ignoredups $ ls $ ls -al $ ls $ ls $ history 2 export HISTCONTROL=ignoredups 3 ls 4 ls -al 5 ls 6 history

code-block:: bash

HISTCONTROL=ignoreboth

Combines the two options above.

This variable’s function is superseded by HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.

HISTFILE

The name of the file in which command history is saved. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits.

code-block:: bash

export HISTFILE=”/dev/null”

Disable history only for current session (history is written to HISTFILE only on logout).

HISTSIZE

code-block:: bash

export HISTSIZE=0

Disable the usage of history using HISTSIZE only for current session.

SAVEHIST

code-block:: bash

export SAVEHIST=0

History won’t be stored in history file after closing current invocation of the shell you’re using. By adding the following line to one of your start-up files (say, ~/.bashrc), you will disable saving history file:

HISTIGNORE

A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is tested against beginning of the line after the checks specified by HISTCONTROL are applied. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

code-block:: bash

export HISTIGNORE=”pwd:ls:ls -al:”

code-block:: bash

$ pwd $ ls $ ls -al $ ls * $ history …. 4 export HISTIGNORE=”pwd:ls:ls -al:” 5 ls *

HISTTIMEFORMAT

code-block:: bash

$ export HISTTIMEFORMAT=»%F %T « $ history 8 2011-04-03 17:43:04 export HISTTIMEFORMAT=»%F %T « 9 2011-04-03 17:43:08 history

BONUS: Clear all the previous history entries in session

Sometimes you may want to clear all the previous history in session, but want to keep the history moving forward and history in HISTFILE. For such an operation you can use command:

code-block:: bash

# history -c

BONUS: Run historic commands

  1. Run some commands

$ dig blog.0x1fff.com

; <<>> DiG 9.7.1-P2 <<>> blog.0x1fff.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42703
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;blog.0x1fff.com.  IN A

;; ANSWER SECTION:
blog.0x1fff.com. 64 IN CNAME ghs.google.com.
ghs.google.com.  415851 IN CNAME ghs.l.google.com.
ghs.l.google.com. 102 IN A 74.125.43.121

;; Query time: 39 msec
;; SERVER: 192.168.10.1#53(192.168.10.1)
;; WHEN: Sun Apr  3 17:46:18 2011
;; MSG SIZE  rcvd: 94
$ dmesg|head -n 2
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
  1. Display commands history

$ history
...
13  2011-04-03 17:45:39 dmesg|head -n 2
15  2011-04-03 17:46:18 dig blog.0x1fff.com
16  2011-04-03 17:47:12 history
  1. Run commands again

$ !-2 ; !13
dig blog.0x1fff.com ; dmesg|head -n 2

; <<>> DiG 9.7.1-P2 <<>> blog.0x1fff.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59379
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;blog.0x1fff.com.  IN A

;; ANSWER SECTION:
blog.0x1fff.com. 64 IN CNAME ghs.google.com.
ghs.google.com.  177837 IN CNAME ghs.l.google.com.
ghs.l.google.com. 125 IN A 74.125.43.121

;; Query time: 61 msec
;; SERVER: 192.168.10.1#53(192.168.10.1)
;; WHEN: Sun Apr  3 17:48:08 2011
;; MSG SIZE  rcvd: 94
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu

This may be extremely useful when used with grep.

Comments

comments powered by Disqus