Tag Info

Hot answers tagged

94

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 ...


71

# avoid duplicates.. export HISTCONTROL=ignoredups:erasedups # append history entries.. shopt -s histappend # After each command, save and reload history export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"


67

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 ...


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 ...


45

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 ...


43

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


33

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 ...


32

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)


32

If your commands contain passwords or other sensitive informations


31

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 ...


29

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 ...


28

! 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 ...


28

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 ...


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 ...


26

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 ...


25

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: ...


23

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 ...


23

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 ...


23

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 ...


23

!! 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 !!)"'


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 & ...


21

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 ...


21

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 ...


21

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 ...


20

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 ...


19

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


19

What's going on is that Bash is getting confused about the number of printing characters in your prompt. It sends cursor positioning sequences to the terminal to position the cursor properly for doing command history and such. It needs to have a good idea of where the cursor actually is after printing the prompt. Try setting your prompt to this: ...


19

Add to the following to ~/.inputrc: # Press up-arrow for previous matching command "\e[A":history-search-backward # Press down-arrow for next matching command "\e[B":history-search-forward Explanation ~/.inputrc is the configuration file for GNU readline. Many shells, including bash and tcsh use readline for command line editing. The two lines above will ...


18

If you're running the command over and over and your shell is bash, the HISTIGNORE variable will do this. Lets say you have secret.server.com that you ssh to, FTP files to, etc. that you don't want any line that mentions secret.server.com saved: HISTIGNORE="*secret.server.com*" You can list multiple patterns with a colon separating them. Make sure sure ...


18

I find very useful the following readline commands history-search-backward, history-search-forward (be aware they are different from the usual reverse-search-history, forward-search-history, tied to Ctrl-R, Ctrl-S). I have these commands associated to Ctrl-Up and Ctrl-Down putting the following lines into ~/.inputrc: "\e[1;5A": history-search-backward ...



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