Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
309

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


158

In bash, just !636 will be ok.


150

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


141

reset or tput reset only does things to the terminal. The history is entirely managed by the shell, which remains unaffected. history -c clears your history in the current shell. That's enough (but overkill) if you've just typed your password and haven't exited that shell or saved its history explicitly. When you exit bash, the history is saved to the ...


138

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 use set: Turn Off set +o history Turn on set -o history


113

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


108

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


103

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


96

Running the command with Ctrl+o instead of Enter will run a command from history and then queue up the next one instead of returning to the front of the bash history.


79

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


77

What you are looking for is CtrlR. Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands. To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/...


73

Yes, it's called "history expansion." See LESS='+/^HISTORY EXPANSION' man bash for full details. Using an exclamation point followed by a number is arguably the simplest use: !636 However, you can also run the last executed command directly without knowing its history number: !! Or you can run two commands back: !-2 The form I most often use, ...


72

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


69

You can use tput reset. Besides reset and tput reset you can use following shell script. #!/bin/sh echo -e \\033c This sends control characters Esc-C to the console which resets the terminal. Google Keywords: Linux Console Control Sequences man console_codes says: The sequence ESC c causes a terminal reset, which is what you want if the screen is ...


60

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


60

Use \[...\] around the parts of PS1 that have length 0. It helps bash to get the length of the prompt right. Even with this measure, your command line can get spoiled when using multibyte characters (at least mine does). Hitting Ctrl+L also helps in such cases (but clears the screen at the same time).


59

Using Bash, I would just visit the directories: $ cd /path/to/source/directory $ cd /path/to/destination/directory Then, I would use the shortcut ~-, which points to the previous directory: $ cp -v ~-/file1.txt . $ cp -v ~-/file2.txt . $ cp -v ~-/file3.txt . If one wants to visit directories in reverse order, then: $ cp -v fileA.txt ~- $ cp -v fileB.txt ...


58

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


56

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


52

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


51

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


51

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


49

echo $HISTCONTROL ignoreboth man bash: HISTCONTROL A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. ...


49

Instead of history, you can use fc, which allow you select range: fc -l 4 7


47

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


47

If I recall correctly, you need to explicitly set it, even with bindkey -v. Use something like this: bindkey -v bindkey '^R' history-incremental-search-backward


46

To clear all your history, use history -c To delete a single line, use history -d linenumber


46

Most shells that have a command line editing feature support Emacs key bindings. (a tiny subset) Up Ctrl+P Down Ctrl+N Left Ctrl+B Right Ctrl+F Home Ctrl+A End Ctrl+E Delete Ctrl+D Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc).  Then, for example, ...


44

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


43

With setopt histignorespace, the command is removed from the current session history. If you tested by pressing Up and seeing that the command line is still there, it's a feature. Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to ...


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