Project 48. Customize Command History"How do I prevent sensitive commands from being recorded in Bash history?" This project looks at the Bash history mechanism and how to customize its behavior. Examples show you how to increase history size, merge histories from simultaneous Terminal sessions, and control what's recorded in the history books. It introduces the history command. Learn More
Tip
Recall HistoryBash saves every command you type in its command-line history. Press the up arrow (Cursor-Up) key to recall previous command lines, and the down arrow (Cursor-Down) key to move in the opposite direction. View the history by using Bash's built-in history command. To limit what's displayed to the last n commands, pass an argument n. $ history 5 514 cat ~/.bash_history 515 echo $HISTFILE 516 shopt 517 help history 518 history 7 To discover more about the history command, type $ help history When you exit a shell, all the commands you entered during the session (up to a configurable maximum number) are saved to a file called .bash_history in your home directory. When you start a new shell session, those commands are read back into the command-line history. The last 500 commands issued in the current shell session are remembered and saved to the history file. Should you wish to change this value, change the shell variable HISTSIZE. Here, we increase the value to 5,000. Tip
$ HISTSIZE=5000 The maximum size of the history file is defined separately by the shell variable HISTFILESIZE. Change it from the default value of 500 to 5,000 by typing Learn More
$ HISTFILESIZE=5000 Change HistoryBe the envy of politicians and rewrite the history books. Disable HistoryShould you have something to hide, you can switch off the history mechanism by unsetting the shell attribute history. $ set +o history To switch it back on again, type $ set -o history Hide HistoryWhen you issue the same command two or more times in succession, the duplicate lines are added to the history. This is pointless, so fix it by typing $ HISTCONTROL="ignoredups" Of particular use is the ability to prevent certain commands from being written to the history. We have two options: prevent all commands that start with a space character and/or prevent consecutive duplicate commands. To hide commands that start with a space, we set HISTCONTROL to ignorespace. To enable both this feature and ignore duplicates, type $ HISTCONTROL="ignoreboth" Here's an example in which we mount an AFP (Apple File Sharing) volume. The command line to do so includes a password, mypass. To prevent this password from appearing in the command-line history and from being written to the history file, we must issue the command by typing a space character as the first character on the command line. $ mkdir s-on-c $ mount_afp afp://saruman:mypass@carcharoth/saruman ~/s-on-c mount_afp: the mount flags are 0000 the altflags are 0020 $ umount s-on-c/ When we view the history, we do not see the mount_afp command. $ history ... 22 HISTCONTROL="ignoreboth" 23 mkdir s-on-c 24 umount s-on-c/ 26 history We can also tell Bash to prevent a whole class of commands from being recorded. To do this, we set the HISTIGNORE shell variable to a colon-separated list of commands to ignore. For example, to prevent all command lines that start with sudo or mount from being recorded, type $ HISTIGNORE="sudo*:mount*" Each pattern must either match the entire command line or finish with a star character. Merge HistoryIf you're in the habit of running many shell sessions at the same time, you'll want to ensure that as each shell is closed, it merges its history into the history file. Normally, a shell overwrites the existing file, thus losing the most recent commands from any shells closed earlier. To enable merging, we switch on shell option histappend (history append) by typing $ shopt -s histappend Tcsh Shell HistoryThe Tcsh shell has history-size and history-merge features similar to those in Bash. To increase history size, type % set history = 2000 To enable history merging and change the history file size too, type % set savehist = (2000 merge) |