Tag Info

Hot answers tagged

126

So, this is all my history-related .bashrc thing: export HISTCONTROL=ignoredups:erasedups # no duplicate entries export HISTSIZE=100000 # big big history export HISTFILESIZE=100000 # big big history shopt -s histappend # append to history, don't overwrite it # Save and reload the history after each ...


117

# Avoid duplicates export HISTCONTROL=ignoredups:erasedups # When the shell exits, append to the history file instead of overwriting it shopt -s histappend # After each command, append to the history file and reread it export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"


73

When you want bash to stop logging your commands, just unset the HISTFILE variable: HISTFILE= All further commands should then no longer be logged to .bash_history. On the other hand, if you are actually supplying passwords as arguments to commands, you're already doing something wrong. .bash_history is not world-readable and therefore not the biggest ...


59

Here is my attempt at Bash session history sharing. This will enable history sharing between bash sessions in a way that the history counter does not get mixed up and history expansion like !number will work (with some constraints). Using Bash version 4.1.5 under Ubuntu 10.04 LTS (Lucid Lynx). HISTSIZE=9000 HISTFILESIZE=$HISTSIZE ...


47

! is a feature that originally appeared in the C shell, back in the days before you could count on terminals to have arrow keys. It's especially useful if you add the current command number to the prompt (PS1="\!$ ") so you can quickly look at your screen to get numbers for past commands. Now that you can use arrow keys and things like Ctrl-R to search the ...


47

Command lines are not just available in history. They are also available, for example, in the output of ps -ocmd or through the /proc filesystem. (/proc/<pid>/cmdline) which is where ps reads them. Also, users' home directories are often world- or group- readable; you can make the history file only user-readable, but that might not survive deletion ...


44

Another tip: I sometimes use comments to bookmark/tag a command: my_command #bookmark then: [ctrl-r]#bookmark


43

Preventative measures If you want to run a command without saving it in history, prepend it with an extra space prompt$ echo saved prompt$ echo not saved \ > # ^ extra space For this to work you need either ignorespace or ignoreboth in HISTCONTROL. For example, run HISTCONTROL=ignorespace To make this setting persistent, put it in your ...


38

With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh): !! Then Enter. Or alternatively: !-1 Then Enter. Or Ctrl+P, Enter Magic space Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late). If using bash, you can put bind Space:magic-space into ~/.bashrc, then ...


37

Ctrl+R is usually the best way, as descriptor said. You can also use !string, which runs the most recent command starting with string, or !?string?, which runs the most recent command that contains string. (I think that's the only stuff relevant to this question, but I covered much more of the history commands in this answer)


37

Bash maintains the list of commands internally in memory while it's running. They are written into .bash_history on exit: When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to the file named by $HISTFILE If you want to force the command history to be written out, you can use the history -a command, which will: ...


35

HISTCONTROL=ignorespace If this option is not already set for bash, it may be just what you need. It is less debilitating than disabling all history. With that set, any commandline starting with a space character will not be saved to the history list. From these related links: Why does bash have a HISTCONTROL=ignorespace option? Why is bash not storing ...


34

This is actually a really interesting behavior and I confess I have greatly underestimated the question at the beginning. But first the facts: 1. What works The functionality can be achieved in several ways, though each works a bit differently. Note that, in each case, to have the history "transferred" to another terminal (updated), one has to press Enter ...


33

!! is expanded by bash when you type it. It's not expanded by alias substitution. You can use the history built-in to do the expansion: alias sbb='sudo $(history -p !!)' If the command is more than a simple command (e.g. it contains redirections or pipes), you need to invoke a shell under sudo: alias sbb='sudo "$BASH" -c "$(history -p !!)"'


32

You might want $HISTIGNORE: "A colon-separated list of patterns used to decide which command lines should be saved on the history list." This line in your ~/.bashrc should do the job: HISTIGNORE='rm *:svn revert*' Also, you can add a space at the beginning of a command to exclude it from history. This works as long as $HISTCONTROL contains ignorespace or ...


32

If your commands contain passwords or other sensitive informations


31

In ZSH: First set HIST_IGNORE_SPACE in your profile and then prefix the commands you don't want stored with a space. From the man page, the following 3 options can be used to say that certain lines shouldn't go into the history at all HIST_IGNORE_SPACE don't store commands prefixed with a space HIST_NO_STORE don't store history (fc -l) command ...


30

You can access this via the forward-search-history function which is bind per default to ctrl+s. Unfortunately ctrl+s is used to signal xoff per default which means you can't use it to change the direction of the search. There are two solutions for solving the problem, one disabling sending the xoff/xon signaling and the other change the keybinding for ...


30

Basically, it's the last argument to the previous command. !$ is the "end" of the previous command. Consider the following example: We start by looking for a word in a file: grep -i joe /some/long/directory/structure/user-lists/list-15 if joe is in that userlist, we want to remove him from it. We can either fire up vi with that long directory ...


29

You can run bindkey with no arguments to get a list of existing bindings, eg: # Enter vi mode chopper:~> bindkey -v # Search for history key bindings chopper:~> bindkey | fgrep history "^[OA" up-line-or-history "^[OB" down-line-or-history "^[[A" up-line-or-history "^[[B" down-line-or-history In emacs mode, the binding you want is ...


28

Pressing Ctrl+R will open the reverse history search. Now start typing your command, this will give the first match. By pressing Ctrl+R again (and again) you can cycle through the history. mysq(Ctrl+R) Would give: mysqldump --add-drop-table -e -q -n -C -u Ctrl+R again: mysql -u ben.dauphinee -p


26

Another usage is for commands that you don't want to accidentally repeat, such as rm -rf *. I make extensive use of history and occasionally hit Enter accidentally when the command I've retrieved from history is not the one I was looking for. Granted, the real solution is to always read commands carefully before executing them. But being a bit clumbsy, I ...


25

I'm not aware of any way using bash. But it's one of the most popular features of zsh. Personally I prefer zsh over bash so I recommend trying it. Here's the part of my .zshrc that deals with history: SAVEHIST=10000 # Number of entries HISTSIZE=10000 HISTFILE=~/.zsh/history # File setopt APPEND_HISTORY # Don't erase history setopt EXTENDED_HISTORY # Add ...


24

This should be what you're looking for: unset HISTFILE From man bash If HISTFILE is unset, or if the history file is unwritable, the history is not saved. Alternatively, if you want to toggle it off and then back on again, it may be easier to do: Turn Off set +o history Turn on set -o history


23

Short answer: Type this at the prompt: $ kill -9 $$ This will kill your shell right away without the shell being able to do anything such as trap the signal, save history, execute ~/.bash_logout, warn about stopped jobs, or any of that good stuff. Long answer: Note: These options are not mutually exclusive; they can be all used at once. Option 1: If ...


23

If you just want the last argument from the previous command then use !$. If you want all the arguments from the last command then use !*. Example COMMAND #1: $ echo 1 2 3 4 1 2 3 4 run #1 then this: $ echo !$ echo 4 4 run #1 then this: $ echo !* echo 1 2 3 4 1 2 3 4 Also I highly recommend that you check out this Unix & Linux Q&A & ...


22

Zsh stores input lines (possibly with time information) in the file indicated by the variable HISTFILE. This should be an absolute file name (otherwise it will be interpreted relative to whatever directory is current at the time). Zsh has no built-in default value for HISTFILE. The zsh distribution comes with a setup wizard for new users which has the value ...


21

You can use the !!:gs/search/replace/ notation to do what you want. This utilizes the global search & replace (:gs): before $ echo "harm warm swarm barm" harm warm swarm barm after $ !!:gs/arm/orn/ echo "horn worn sworn born" horn worn sworn born References The Definitive Guide to Bash Command Line History Caret search and replace in Bash shell ...


20

To expand on what Gilles said, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward: # Key bindings, up/down arrow searches through history "\e[A": history-search-backward "\e[B": history-search-forward "\eOA": history-search-backward "\eOB": history-search-forward Just type something ...


20

If it refers to commands run just recently, a more efficient way is to reference them with negative numbers: !-4; !-3; !-2; !-1 Also, once you do it, your last history entry will contain the whole chain of commands, so you can repeat it with !!. Edit: If you haven't already, get familiar with the great builtin function fc, mentioned by Gilles. (Use ...



Only top voted, non community-wiki answers of a minimum length are eligible