Tag Info

Hot answers tagged

80

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


46

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


33

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


31

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)


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

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


25

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


25

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


24

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


22

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

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


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

I'm not aware of any way using bash. But it's one of the most popular features of zsh. Personally I prefere 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 ...


19

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


19

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


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


18

bash actually remembers the times until you close the shell. So try running HISTTIMEFORMAT='%x %X ' history If you also put HISTTIMEFORMAT=<some format> in your ~/.bashrc, it will also get written to ~/.bash_history on exit, so you can check what happened in previous shell sessions too.


17

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


17

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


16

Your shell's history is saved in the file indicated by the HISTFILE variable. So: unset HISTFILE This also applies to zsh, but not to ksh which keeps saving to the file indicated by $HISTFILE when the shell starts (and conversely, you decide to save your history in ksh once you've started the shell).


16

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


16

Using !$ should work to access the last argument of the previous command in the bash shell: less super/long/file/name vim !$ Also Meta + . or Esc + . can be used to paste the last argument if the readline library is enabled in emacs mode (default option).


16

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


16

If you search history via Ctrl+r and typing some letters of the command and not pressing Enter but pressing →, the command will show up and will not run. Another alternative is: history | grep 'mycommand' This nice history cheatsheet might also help.


15

Passwords on the command line are just a bad idea all the way around. In addition to the methods discussed in the other answers: /proc process list (ps) user's history file User commands can show up in these locations as well: audit logs /var/log/* In addition user's commands can also show up when users login between systems, so in general it's a bad ...


14

To request that the command be printed rather than executed after history substitution, add the :p modifier, e.g. !42:p. The resulting command will also be entered in the history, so you can press Up to edit it. If you have the histverify option set (shopt -s histverify), you will always have the opportunity to edit the result of history substitutions. The ...


13

In bash, use the HISTCONTROL variable. Set it to HISTCONTROL=ignorespace (or HISTCONTROL=ignoreboth). From now, when you begin a line with a space and it will not be saved in the history. This avoids to include the not-to-be-disclosed-command in some configuration file. Even like that it happens to forget to add the space and then want to go back. To ...


13

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


12

The a attribute means that the file is append-only: you can't overwrite it or delete it, only append data to it. This is explained in the chattr man page. Only root can remove the attribute. The practical consequence is that you can't erase your old history lines. This is presumably intended as a security measure by your system administrator. I'm not ...



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