Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

Bash history and bang commands

News See Also Recommended Links Reference Bang commands Bang arguments
 History Controls Searching history Event Designators Word Designators Modifiers Examples
Command completion Readline and inputrc Input and Output Redirection Customizing Shell Dot Files: .profile, RC-file, and history Examples of .bashrc files Shell Prompts Customarization
Pushd, popd and dirs as a proxy for "cd history" Orthodox File Manager Paradigm. Ch. 4 Norton Change Directory (NCD) clones Advanced filesystem navigation

fc command


Care and Feeding of Functions in Shell bash Tips and Tricks Unix shells history Sysadmin Horror Stories Humor Etc
 
"Those who cannot remember the past are condemned to repeat it."
 

George Santayana (1863-1952)


Introduction

Famous quote "Those who cannot remember the past are condemned to repeat it"  has pretty literal meaning when you are using Unix shell in interactive mode :-). All modern shells have some mechanism of working with the history of commands that was originally introduced in C-shell.

To retrieve history  history command are usually used. History command in various forms is present in all modern shells. It is operated on history file and memory buffer with recent commands. History file is stored in a regular flat file and this file can be read and processed with standard Unix tools too, but by read by history command. For example grep.

Bash provides an additional (and rather important in enterprise environment where there are multiple cooks int the kitchen) opportunity to timestamp history command. Bash history  command can list all or part of the history file. It is only available in interactive sessions: it is disabled within scripts and you need to process history file is you need to get some commands from it. 

The location of the history file is defined by the value of the environment variable HISTFILE.  Its redirection from default location is somewhat tricky business as BASH is buggy in this respect. For example if you redirect root history to your user directory in recent versions of bash you lose the ability to view history with history command although file will be written correctly.

Shell approach to keeping history on commands now looks outdated.  Some important features like timestamp in bash were added as an after thought and not well integrated. Also "universal" shell  history concept is open to review. There are many different "pre command" or per "set of command" histories that are interesting along with "total history". First of all the history of visited directories and history of opened for editing  or listing files.  Bash is unable to provide such slices of command history. You can do it using grep or Perl but there are no any built-in features that provide those or similar capabilities. If you alias cd command to pushd then directory stack will be your proxy for history of visited directories. You also can write hostiry of visited directoriez yourself by integrating

echo $PWD > ~/.dir_history 

or similar command into your PROMPT_COMMAND function. Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

That means that there are several different avenues of making command hostory more useful:

Postprocessing the history file

Using shell or Perl (or any other scripting language that comfortable to you) you can write various useful postprocessors that provide relevant slices of history. For example, to see which commands you type most often and convert some of them into aliases you can use:

cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -12

After analyzing the list you can can create a set of directories favorites using aliases staring with a special letter, for example the letter "2":

alias 2log='cd /var/log'
alias 2netconf='cd /etc/sysconfig/networking'

Modem implementations of file managers like you can find in various OFMs, such as mc, have those capabilities built-in. That means that you can use screen with the second screen being OFM file manager and use this second screen for navigating. Synchronizing of directories between screens can be accomplished within PROMPT_COMMAND function, if you export a variable then represents the current directory from mc. For example, FAR maintains both history of opened files and the history of visited directories. For files and directories OFMs usually have  ways to organize permanent list of frequently used items similar to favorites in browsers. 

Making full use of bang command

Most unix shells including bash have some interesting extensions of the command history called bang commands. They allow you not only quickly retrieve  several most recent commands but also reuse parts of previous command (or any command within history). Some of bang command like !$ (the last operand of the previous command) are extremely useful. More convoluted bang operations are often simpler to perform just listing history and selecting the necessary substring using the mouse. Moreover, other that five major bang commands (!!, !!:h, !!:f, !$, !1) it's actually difficult to remember those capabilities as their usage is pretty infrequent.  We will discuss them in more details later.

History Controls

Bash saves all the commands entered from your terminal device to a history file. Default location of the history file is ~/.bash_history.  It can be overwritten using HISTFILE variable.  But again this capability is very buggy if you try to do it for root history file.

If the HISTFILE variable is not set or specified or if location cannot be written, the default name is used.

Actually this file should be treated not as a flat file but as a database. Generally history file should be append-only. That prevent mishaps when it is accidentally truncated. This can be accomplished by issuing the following command:

chattr +a ~joeuser/.bash_history

Control of the behavior of the command history is provided jointly by bash and the readline library. In addition to classic veriable ($HISTFILE Bash provides four other variables that control what information is written to history( $HISTTIMEFORMAT, $HISTSIZE, $HISTCONTROL and $HISTIGNORE):

History can be browsed using arrow commands, but more efficient way is to use Ctrl-R  for anything but previous half-dozen of commands. Ctrl-R  mechanism provides search in history:

Most important subset of band commands

There are way too many bang command to remember them all. But several most common cases ahoul in the the arsenal of any sysadmin, who respects himself. I would outlike seven most important usage cases:  

  1. !! --Will execute the last command. Same as !-1 or "up-arrow + return"
  2. !n --Will execute the line n of the history record. 
  3. !-n --Will execute the command n lines back. 
  4. !gzip  -- will re-execute the most recent gzip command (history is searched  in reverse order). string specified is considered to be prefix of the necessary command.
  5. !?etc.gz – same as above but the unique string doesn't have to be at the start of the command. That means that you can identify command by a unique string appearing anywhere in the command  (exactly like in Ctrl-R  mechanism)
What is more important and convenient is that you can extract arguments from previous commands  (see below). for example:
  1. !!:1 designates the first argument of the last command.  This can be shortened to !1.
  2. !!:$ designates the last argument of the preceding command. This may be shortened to !$.

 There are a couple of useful idioms:

 Retrieving previous command arguments

While using mouse is usually more convenient, sometimes it make sense to retrieve the previous commands arguments using so called word designators or bang arguments.  A colon (:) separates the event designator and the word designator. If the word designator begins with $, %, ^, *, or - the : is not needed. If you want the word to be selected from the previous command line, the second ! is not required. For example, !!:2 and !:2 both refer to the second word on the previous command.

You can address the necessary argument by using words, lines count from the last and search strings

Here is a more compete reference:

Built-in command for searching history (Ctrl-R and Ctrl-S)

There are two ways to three ways of searching history of commands: linear, incremental and with grep.

An interesting,  but lost in recent versions of SUSE, capability of searching

In Suse there is also an interesting possibility to use page-up/page-down commands in searching (Page-up-Page-down keys and shell history):

On Sat, 5 May 2001, Joost van der Lugt wrote:

 Hi all,
 
 I've been looking into this for a little bit and am unable to find an answer. This is a feature AFAIK only implemented by SuSE:
 
 At a prompt type one or more characters then use Page Up/Page Down  to scroll through all commands that started with these characters.
 So typing:

 cd 

would then give a 'scrollable' list of anything that started with cd  simply hit enter when you reach the command you were looking for.
 
 I know Ctrl-r and am familiar with ! but they are not as nice, and  really not the same.  Would anybody have a clue as to how this could be implemented?
 
 I don't think it is shell dependent, so I don't think it's a bash  option but I could very well be wrong.
 (Tried the dotfile generator, but don't see anything there)
 
 The option is _really_ addictive therefore my post:)
 Thanks,
 
 --
 Cheers,
 
 Joost van der Lugt

Problem of multiple administrators on the same server

Shell history issues became more complex. if there are multiple administrators on a particular server.

One of the ways to solve this problem is use sudo. The other is to use individual .profile after switching to root that allows to individualize and relocate your history file.

But in this case you will lose the ability to track actions of other admins on the server.  

Creeping featurism vs situational awareness problem

Different people learn different patterns of work with shell, so the set of features one adopts is usually quite idiosyncratic. For example, I seldom use internal search of command history in the shell and prefer to use external history search via grep (I created special a function for that)

Drastic equipment changes during 45 years of Unix development and problem within the idea of "pure" command line shell itself (with first features design to teletypes). This long and convoluted history of development od shell that includes several forks (tcsh, zsh, ksh93) naturally led to the situation when there are just too many features to learn in Unix shells. The idea of borrowing the best from each other work only so far.

And if there way too many features, the features that you do not use often, you forget pretty soon, even if you wrote a web page about them ;-). Each time I edit this page I wonder how I manage forget so much about the shell history and why I use so few of the capabilities I myself describe on this page. Bu there is a reason in it: intellectual capacities of humans are limited and Unix taxes them to the fullest expect. It is a classic example of Os that is too complex for "normal humans" to use. So creating a tiny subset is a natural defensive reaction against all this complexity. Many of the features that were essentials of classic teletype now are much less so when you have work from window GUI using emulator like Putty of Teraterm and mouse is supported in both environments. 

But there is a fundamental feature that you should never forget: the less you type, the less mistakes you make. see Sysadmin Horror Stories about what can happen if you accidentally mistype some command. So maximizing reuse of "destructive" as well as other command is a legitimate goal in those circumstances.  So maximal reuse of history should be the goal not because it make you work easier (after certain, minimal, let's say 20% mastery of features) but because the more often you use history capability in shell the better is your "situational awareness is". And that means that there are less chances that you in a hurry or because you is being tied commit some horrible command like blunder and then face SNAFU recovering from which might take a day or two.

So diligently try I relearn useful methods of reusing the history and then, as usual, forget them in a couple of months. And this cycle looks like an infinite loop.  In this sense the famous quote "Those who cannot remember the past are condemned to repeat it" has a different, more menacing meaning: Bash does suffer from Creeping featurism:

1. Describes a systematic tendency to load more chrome and features onto systems at the expense of whatever elegance they may have possessed when originally designed. See also feeping creaturism. "You know, the main problem with BSD Unix has always been creeping featurism."

2. More generally, the tendency for anything complicated to become even more complicated because people keep saying "Gee, it would be even better if it had this feature too". (See feature.) The result is usually a patchwork because it grew one ad-hoc step at a time, rather than being planned. Planning is a lot of work, but it's easy to add just one extra little feature to help someone ... and then another ... and another.... When creeping featurism gets out of hand, it's like a cancer.

Usually this term is used to describe computer programs, but it could also be said of the federal government, the IRS 1040 form, and new cars. A similar phenomenon sometimes afflicts conscious redesigns; see second-system effect. See also creeping elegance.

Here is more modern definition from Wikipedia:

Creeping featurism, or creeping featuritis, is a phrase used to describe software which over-emphasizes new features to the detriment of other design goals, such as simplicity, compactness, stability, or bug reduction. The term is often spoonerized as feeping creaturism, a term intended to bring the image of the encroaching features as small, seemingly benign animals that creep into a program, making their trademark noises and demanding their own space, soon taking over the entire project.

Creeping featurism is often accompanied by the mistaken belief that "one small feature" will add zero incremental cost to a project, where cost can be money, time, effort, or energy. A related term, feature creep, describes the tendency for a software project's completion to be delayed by the temptation to keep adding new features, without a specific goal.

Due to creeping featurism there are some built-in limits in productivity achievable with any old technology.

Alternative ways of improving your productivity with bash

Often the way to raise productivity is to switch to a newer technology.  There are several way to use bash more productively and less rely on retrieving old commands for enhancing your productivity. Among them:

Using orthodox file managers

If you are interested is using shell history you might be also interested in orthodox file managers that represent visual extension of the shell and as such greatly simplify the work of system administrator and tremendously raise the productivity. They are not perfect and not for all but to determine this you need to try them for a at least a couple of weeks.

For Linux the most popular orthodox file manager is probably Midnight commander. You can also always use two sessions: one with Orthodox file manager and another with regular shell and switch between two depending on the operation you need to perform. Browsing the tree and copy move operation are so much efficient in orthodox manager that you can feel real disappointment that you did not learn them after. Also browsing of the tree is simple and more visual. to get to the same directory in your second session export an environment viable if you are root or write file with the current directory and then source this file in other session.  

So while each time I read this page with great interest, each time I try to remind myself that reusing the command that already work can prevent wiping out some important directory due to a typo; action that I already committed several time in the past  :-).  that's actually why using OFM file manager such as mc is a better way to work with Unix filesystem and no level of command line skills can compensate for missing GUI-style feedback they provide.

Screen can be combined with OFM (OFM manager can used in a separate screen). This is the environment I consider the most productive and strive to use as often as I can older versions of mc such as 4.6 which is "standard de facto" in many distributions has pretty primitive emulation of the terminal.

Use of screen

Another important way to improve your productivity is to learn to use GNU screen. It permit using multiple screens instead of switching to multiple directories and as such increase sysadmin productivity and lessens the number of mistakes.

The problem it solves is only peripherally related to the bash history, and can be described as following: many command that regular sysadmin types are connected with the nessisty to work in two or more different directories. Having multiple screens (one for each directory) eliminate the necessity to type innumerous cd and ls commands.  Screen also solves the problem of esptablisng your environment each time you login to particular server. and it preserves your session in case of accidental disconnects. In other words it is a must tool. I always pity "old style" Unix sysadmin that do this day after day. There are much more interesting ways to get RSI , for example playing computer games  ;-)

Using programmable keyboards

And the last, but not least was to enhance capabilities of bash history is to use programmable keyboards. This is especially convenient if you are using terminal emulator like Teraterm. Teraterm macros are also very helpful for automating repretative tasks for example loggin in to the server and establishing your environemtn (altouth the latter problem is solved by screen)

Dr. Nikolai Bezroukov


Top updates

Softpanorama Switchboard
Softpanorama Search


NEWS CONTENTS

Old News ;-)

Note: A highly recommended shell site is SHELLdorado by Heiner Steven.
This is a really excellent site with the good coding practice section,
some interesting example scripts and tips and tricks

[Nov 08, 2015] Get timestamps on Bash's History

nickgeoghegan.net
One of the annoyances of Bash, is that searching through your history has no context. When did I last run that command? What commands were run at 3am, while on the lock?

The following, single line, run in the shell, will provide date and time stamping for your Bash History the next time you login, or run bash.

echo  'export HISTTIMEFORMAT="%h/%d - %H:%M:%S "' >>  ~/.bashrc

Using Bash's History Effectively

Okay, well say you didn't just run the sought-after command. You know you've used it within the past few days, but you don't want to scroll through what could be hundreds of commands to find it. Well, there are a couple of ways to do this, depending on how much you can remember about the history yourself...

Remember: Once you've used one of these methods to recall and execute a command, that command is now the most recent command in your history. You can now just press the up arrow once to retrieve it again.


Okay, so now you can recall commands with relative ease. But we really haven't done anything special yet. We're just using the default behaviors and commands provided in Bash. The trick comes in configuring what gets put into your history!

A little-known trick (I've only seen one distro ship with this turned on by default) is that you can filter what gets stored in the command history. This is done merely by setting an environment variable, $HISTIGNORE.

Just set the HISTIGNORE variable to a colon-delimited list of patterns which should be excluded. There's also the special pattern '&', which suppresses duplicate entries (very useful!).

Here's an example that suppresses duplicate commands, the simple invocation of 'ls' without any arguments, and the shell built-ins bg, fg, and exit:

Try it out. I think you'll like it.

Here's a neat trick, submitted by Robert Cymbala. If you include the expression "[ \t]*" in the HISTIGNORE string, you can suppress history recording at will for any given command just by starting with a space!


Another optional feature of Bash history filtering that isn't always enabled by default in distributions is cmdhist. This determines whether multi-line commands are stored in the history as a single command (on) or not (off; default). This makes it much easier to go back and edit multi-line commands.

To enable this feature, you would type

To disable this feature, you would type

As you may have guessed, there are many options for Bash that can be toggled using the 'shopt' built-in. They are documented here.

[Jun 07, 2011] BASH history forever

I would like to keep track of what, when and where I've done something in the shell for the rest of my Linux life. It is a reasonable wish to have all of my activities logged, so in the future I could check what I did, how I did, and when I did it. Of cause it imposes some security hazard if you type in your password by mistake while working in the shell prompt, so you should be careful and have right permissions setup.

This article is excerpt from its original page where you can see how it evolved ;-)

The first solution would be to set HISTSIZE to be very big, but then I don't know how well your bash would behave -- I believe it tries to keep them all in memory. But I want to have my bash fast and lightweighted! So it must be accomplished in another way.

Lets use a big file ~/.bash_history.archive (separate from HISTFILE=~/.bash_history). And then on exit from each bash session lets append new history lines to it. To accomplish that we need to remember how far in the history we were at the beginning of the session and at the end of session, so we could dump lines inbetween to our log file. The problem I've ran into is that it is impossible at the time of run of .bash{rc,_profile} to know status of history, thus I ended up scanning HISTFILE, thus I still can have some bugs because this way is unnatural.

To enable such historing you need merely to source a .bash_history from your ~/.bashrc. Optionally you can modify .inputrc to have shortcut to dump history without exiting shell. I provide source of the script directly in the article so it is available even if my website goes down (I hope it will not in the nearest future)

#!/bin/bash
#-------------------------- =+- Shell script -+= --------------------------
# @file      .bashrc_history.sh
# @date      Thu Mar 10 14:02:36 2005
# @brief
#
# CVS version control block - do not edit manually
#  $RCSfile: .bashrc_history,v $
#  $Source: /home/cvs/yoh/.bashrc_history,v $
#
# Created: Thu Mar 10 14:02:36 2005
#  Commited: $Date: 2005/03/24 14:24:28 $
#  Revision: $Revision: 1.7 $
#
#  Yaroslav Halchenko                                      CS@UNM, CS@NJIT
#  web:         http://www.onerussian.com                      & PSYCH@RUTGERS
#  e-mail:  yoh@onerussian.com                              ICQ#: 60653192
#
# DESCRIPTION (NOTES):
#   A script to be sourced from .bashrc to provide ways to archive all the
#   actions in infinitely long history file.
#  
#   To use, just place
#     'source .bashrc_history'   in ~/.bashrc
#
#     '$if Bash
#      # to exit through calling exit function which will archive the history
#      # next ones are optional: first is left for historical reasons
#      "\C-x\C-x": "exit\n"
#      "\C-x\C-w": "archive_history\n"
#      $endif'                   in ~/.inputrc
#
#   Then whenever you close bash (exit,logout or exit by pressing Ctrl-D
#   or Ctrl-X twice you will have a piece of current history added to
#   ~/.bash_history.archive
#
# SOURCE:
#     http://www.onerussian.com/Linux/.files/.bashrc_history
#
# LICENSE: 
#   Released under GNU Generic Public License. You should've received it with
#   your GNU/Linux system. If not, write to the Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#-----------------\____________________________________/------------------


if [ "$PS1" ] ; then # interactive shell
    export  \
	CURBASHSTART=$(grep -v -e "^[ \t]*$" -e "^\s*#" $HISTFILE 2>/dev/null /dev/null) \
	CURBASHDATE=$(date > $HISTORYOLD
	    history $(($HISTCMD-$CURBASHSTART-1)) | sed -e 's/^[ ]*[0-9][0-9]* [ ]*//g'  >> $HISTORYOLD
	    CURBASHSTART=$(($HISTCMD-1))
	fi
    }

    trap 'archive_history' EXIT

fi

P.S. There might be much better/safer way to reach the goal -- thus comments are very welcome

[Jun 06, 2011] Bash history cheatsheet

Task 4 Check command history for “string” but avoid running it

Print the first occurrence of string found in the history, but don’t run it.

!string:p 

!string will search your command history and execute the first command that matches ‘whatever’. If you don’t feel safe doing this put :p on the end to print without executing. Recommended when running as superuser.

Task 5 delete a line of the history

Useful when you do an error and type a password as normal command.

Apply to bash

history -d < history number > 

Delete the entry with the < history number > from the command line history

Task 6 List of commands you use most often

useful in deciding what new alias to do or for the statistics.

Apply to bash and zsh

history | awk '{print $2}' | sort | uniq -c | sort -rn | head

Output:

    53 aptitude
     47 ip
     40 ls
     33 apt-get
     26 cd
     25 xrandr
     24 man
     17 history
     15 rmmod
     15 apt-cache

Check Also the Bash history cheatsheet

[Mar 12, 2010] Bash History Display Date And Time For Each Command

by Vivek Gite · 16 comments

How do I display shell command history with date and time under UNIX or Linux operating systems?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows:

$ HISTTIMEFORMAT="%d/%m/%y %T "
OR
$ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
Where,

%d - Day
%m - Month
%y - Year
%T - Time

To see history type

$ history
Sample outputs:
....
..
  986  11/03/10 04:31:36 memcached-tool  10.10.28.22:11211 stats
  987  11/03/10 04:31:36 w
  988  11/03/10 04:31:37 iostat
  989  11/03/10 04:31:37 top
  990  11/03/10 04:31:37 at
  991  11/03/10 04:31:38 atop
  992  11/03/10 04:31:40 collectl
  993  11/03/10 04:31:41 grep CPU /proc/cpuinfo
  994  11/03/10 04:31:45 vmstat 3 100
  995  11/03/10 04:31:55 sar -W -f /var/log/sa/sa12
....
izaak 03.12.10 at 11:06 am
I would also add
$ echo 'export HISTSIZE=10000' >> ~/.bash_profile 

It’s really useful, I think.

14 Dariusz 03.12.10 at 2:31 pm
you can add it to /etc/profile so it is available to all users. I also add:

# Make sure all terminals save history

shopt -s histappend histreedit histverify
shopt -s no_empty_cmd_completion # bash>=2.04 only

# Whenever displaying the prompt, write the previous line to disk:

PROMPT_COMMAND=’history -a’

#Use GREP color features by default: This will highlight the matched words / regexes

export GREP_OPTIONS=’-color=auto’
export GREP_COLOR=’1;37;41'
15 Babar Haq 03.15.10 at 6:25 am
Good tip. We have multiple users connecting as root using ssh and running different commands. Is there a way to log the IP that command was run from?
Thanks in advance.
16 cadrian 03.16.10 at 5:55 pm
Yup, you can export one of this:
env | grep SSH 
SSH_CLIENT=192.168.78.22 42387 22 
SSH_TTY=/dev/pts/0 
SSH_CONNECTION=192.168.78.22 42387 192.168.36.76 22

As their bash history filename

set |grep -i hist 
HISTCONTROL=ignoreboth >
HISTFILE=/home/cadrian/.bash_history 
HISTFILESIZE=1000000000 
HISTSIZE=10000000

So in profile you can so something like

HISTFILE=/root/.bash_history_$(echo $SSH_CONNECTION| cut -d\ -f1)

[Jul 16, 2008] Advancing in the Bash Shell

16 Jul 2008 | Deadman.org

Bash provides a couple of methods for searching the command history. Both are useful in different situations. The first method is to simply type history, find the number of the command you want and then type !N where "N" is the number of the command you'd like to execute. (:p works here too.) The other method is a tad more complex but also adds flexibilty.

^r (ctrl-r) followed by whatever you type will search the command history for that string.

The bonus here is that you're able to edit the command line you've searched for before you send it down the line. While the second method is more powerful, when doing some redundant task, it's much easier to remember !22 than it is to muck with ctrl-r type searches or even the arrow keys.

Bang dollar-sign

!$ 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/userlists/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 tree as the argument, or as simply as

vi !$
A word of caution: !$ expands to the end word of the previous command. What's a word? The bash man page calls a word "A sequence of characters considered as a single unit by the shell." If you haven't changed anything, chances are good that a word is a quoted string or a white-space delimited group of characters. What is a white-space delimited group of characters ? It's a group of characters that are separated from other characters by some form of white-space (which could be a tab, space, etc.) If you're in doubt, :p works here too.

Another thing to keep in mind when using !$ is that if the previous command had no agruments, !$ will expand to the previous command rather than the most recent argument. This can be handy if, for example, you forget to type vi and you just type the filename. A simple vi !$ and you're in.

Similar to !$ is !*. !* is all of the arguments to the previous command rather than just the last one. As usual, this is useful in many situations. Here's a simple example:
vi cd /stuff
oops!
[exit vi, twice]
!*
Which bash expands to: cd /stuff

... ... ...

A few handy movement commands

Sometimes a mistake is noticed before the enter key is pressed. We've already talked about terminals that don't translate cursor-keys properly, so how do you fix a mistake? To make matters worse, sometimes the backspace key gets mapped to ^H or even worse something like ^[[~. Now how do you fix your mistake before hitting the enter key?

Once again, bash comes through for us. Here are some of the movement keystrokes that I use most often:

There are more of course, but those are the ones you simply can't live without. For those who don't know the ^N notation means ctrl+N, don't confuse it with hats mentioned above.

tab-tab

One of my favorite features of bash is tab-completion. Tab-completion works in a couple of ways, it can complete filenames in the current directory or in your $PATH. Like the !commands above, you just need to give bash enough of the filename to make it unique and hit the tab key -- bash will do the rest for you. Let's say you have a file in your home directory called ransom.note, consider the following:

mor
[tab] ran[tab]

Will expand to

more ransom.note

Let's say you also have a file named random in your home directory. ran above is no longer enough to be unique, but you're in luck. If you hit tab twice, bash will print the list of matching files to the screen so that you can see what you need to add to make your shortcut unique.

Send comments to Sam Rowe, deadman at deadman dot org.

[Apr 30, 2008] CLI Magic Using command history in the bash shell by Mark Sobell

July 03, 2006 | ,Linux.com

fc: Displays, Edits, and Reexecutes Commands

The fc (fix command) builtin enables you to display the history list and edit and reexecute previous commands. It provides many of the same capabilities as the command line editors.

When you call fc with the -l option, it displays commands from the history list. Without any arguments, fc -l lists the 16 most recent commands in a numbered list, with the oldest appearing first:

$ fc -l
1024     cd
1025     view calendar
1026     vim letter.adams01
1027     aspell -c letter.adams01
1028     vim letter.adams01
1029     lpr letter.adams01
1030     cd ../memos
1031     ls
1032     rm *0405
1033     fc -l
1034     cd
1035     whereis aspell
1036     man aspell
1037     cd /usr/share/doc/*aspell*
1038     pwd
1039     ls
1040     ls man-html

The fc builtin can take zero, one, or two arguments with the -l option. The arguments specify the part of the history list to be displayed:

fc -l [ first [ last ]] 

The fc builtin lists commands beginning with the most recent event that matches first. The argument can be an event number, the first few characters of the command line, or a negative number, which is taken to be the n th previous command. If you provide last , fc displays commands from the most recent event that matches first through the most recent event that matches last . The next command displays the history list from event 1030 through event 1035:

fc -l 1030 1035
1030     cd ../memos
1031     ls
1032     rm *0405
1033     fc -l
1034     cd
1035     whereis aspell

The following command lists the most recent event that begins with view through the most recent command line that begins with whereis:

$ fc -l view whereis
1025     view calendar
1026     vim letter.adams01
1027     aspell -c letter.adams01
1028     vim letter.adams01
1029     lpr letter.adams01
1030     cd ../memos
1031     ls
1032     rm *0405
1033     fc -l
1034     cd
1035     whereis aspell

To list a single command from the history list, use the same identifier for the first and second arguments. The following command lists event 1027:

$ fc -l 1027 1027
1027     aspell -c letter.adams01

You can use fc to edit and reexecute previous commands.

fc [-e   editor ] [ first [ last ]]

When you call fc with the -e option followed by the name of an editor, fc calls the editor with event(s) in the Work buffer. Without first and last , fc defaults to the most recent command. The next example invokes the vi(m) editor to edit the most recent command:

$ fc -e vi 

The fc builtin uses the stand-alone vi(m) editor. If you set the FCEDIT variable, you do not need to use the -e option to specify an editor on the command line. Because the value of FCEDIT has been changed to /usr/bin/emacs and fc has no arguments, the following command edits the most recent command with the emacs editor:

$ export FCEDIT=/usr/bin/emacs
$ fc 

If you call it with a single argument, fc invokes the editor on the specified command. The following example starts the editor with event 21 in the Work buffer. When you exit from the editor, the shell executes the command:

$ fc 21 

Again you can identify commands with numbers or by specifying the first few characters of the command name. The following example calls the editor to work on events from the most recent event that begins with the letters vim through event 206:

$ fc vim 206 

When you execute an fc command, the shell executes whatever you leave in the editor buffer, possibly with unwanted results. If you decide you do not want to execute a command, delete everything from the buffer before you exit from the editor.

You can reexecute previous commands without going into an editor. If you call fc with the -s option, it skips the editing phase and reexecutes the command. The following example reexecutes event 1029:

$ fc -s 1029
lpr letter.adams01

The next example reexecutes the previous command:

$ fc -s 

When you reexecute a command you can tell fc to substitute one string for another. The next example substitutes the string john for the string adams in event 1029 and executes the modified event:

$ fc -s adams=john 1029
lpr letter.john01

Using an Exclamation Point (!) to Reference Events

The C Shell history mechanism uses an exclamation point to reference events, and is available under bash. It is frequently more cumbersome to use than fc but nevertheless has some useful features. For example, the !! command reexecutes the previous event, and the !$ token represents the last word on the previous command line.

You can reference an event by using its absolute event number, its relative event number, or the text it contains. All references to events, called event designators, begin with an exclamation point (!). One or more characters follow the exclamation point to specify an event.

You can put history events anywhere on a command line. To escape an exclamation point so that it is treated literally instead of as the start of a history event, precede it with a backslash (\) or enclose it within single quotation marks.

An event designator specifies a command in the history list.

Event designators

Designator Meaning
! Starts a history event unless followed immediately by SPACE, NEWLINE, =, or (.
!! The previous command.
!n Command number n in the history list.
!-n The n th preceding command.
!string The most recent command line that started with string .
!?string[?] The most recent command that contained string . The last ? is optional.
!# The current command (as you have it typed so far).
!{event} The event is an event designator. The braces isolate event from the surrounding text. For example, !{-3}3 is the third most recently executed command followed by a 3.

You can always reexecute the previous event by giving a !! command. In the following example, event 45 reexecutes event 44:

44 $ ls -l text
-rw-rw-r--   1 alex group 45 Apr 30 14:53 text
45 $ !!
ls -l text
-rw-rw-r--   1 alex group 45 Apr 30 14:53 text

The !! command works whether or not your prompt displays an event number. As this example shows, when you use the history mechanism to reexecute an event, the shell displays the command it is reexecuting.

A number following an exclamation point refers to an event. If that event is in the history list, the shell executes it. Otherwise, the shell displays an error message. A negative number following an exclamation point references an event relative to the current event. For example, the command !-3 refers to the third preceding event. After you issue a command, the relative event number of a given event changes (event -3 becomes event -4). Both of the following commands reexecute event 44:

51 $ !44
ls -l text
-rw-rw-r--   1 alex group 45 Nov 30 14:53 text
52 $ !-8
ls -l text
-rw-rw-r--   1 alex group 45 Nov 30 14:53 text

When a string of text follows an exclamation point, the shell searches for and executes the most recent event that began with that string. If you enclose the string between question marks, the shell executes the most recent event that contained that string. The final question mark is optional if a RETURN would immediately follow it.

68 $ history 10
   59  ls -l text*
   60  tail text5
   61  cat text1 text5 > letter
   62  vim letter
   63  cat letter
   64  cat memo
   65  lpr memo
   66  pine jenny
   67  ls -l
   68  history
69 $ !l
ls -l
...
70 $ !lpr
lpr memo
71 $ !?letter?
cat letter
...

Optional - Word Designators

A word designator specifies a word or series of words from an event.

Word designators

Designator Meaning
n The n th word. Word 0 is normally the command name.
^ The first word (after the command name).
$ The last word.
m-n All words from word number m through word number n ; m defaults to 0 if you omit it (0- n ).
n* All words from word number n through the last word.
* All words except the command name. The same as 1*.
% The word matched by the most recent ?string? search.

The words are numbered starting with 0 (the first word on the line -- usually the command), continuing with 1 (the first word following the command), and going through n (the last word on the line).

To specify a particular word from a previous event, follow the event designator (such as !14) with a colon and the number of the word in the previous event. For example, !14:3 specifies the third word following the command from event 14. You can specify the first word following the command (word number 1) by using a caret (^) and the last word by using a dollar sign ($). You can specify a range of words by separating two word designators with a hyphen.

72 $ echo apple grape orange pear
apple grape orange pear
73 $ echo !72:2
echo grape
grape
74 $ echo !72:^
echo apple
apple
75 $ !72:0 !72:$
echo pear
pear
76 $ echo !72:2-4
echo grape orange pear
grape orange pear
77 $ !72:0-$
echo apple grape orange pear
apple grape orange pear

As the next example shows, !$ refers to the last word of the previous event. You can use this shorthand to edit, for example, a file you just displayed with cat:

$ cat report.718
...

$ vim !$
vim report.718
...

If an event contains a single command, the word numbers correspond to the argument numbers. If an event contains more than one command, this correspondence does not hold true for commands after the first. In the following example, event 78 contains two commands separated by a semicolon so that the shell executes them sequentially; the semicolon is word number 5.

78 $ !72 ; echo helen jenny barbara
echo apple grape orange pear ; echo helen jenny barbara
apple grape orange pear
helen jenny barbara
79 $ echo !78:7
echo helen
helen
80 $ echo !78:4-7
echo pear ; echo helen
pear
helen

On occasion you may want to change an aspect of an event you are reexecuting. Perhaps you entered a complex command line with a typo or incorrect pathname, or you want to specify a different argument. You can modify an event or a word of an event by putting one or more modifiers after the word designator, or after the event designator if there is no word designator. Each modifier must be preceded by a colon (:).

The substitute modifier is more complex than the other modifiers. The following example shows the substitute modifier correcting a typo in the previous event:

$ car /home/jenny/memo.0507 /home/alex/letter.0507
bash: car: command not found
$ !!:s/car/cat
cat /home/jenny/memo.0507 /home/alex/letter.0507
...

The substitute modifier has the following syntax:

[g]s/ old _/ new _/

where old is the original string (not a regular expression), and new is the string that replaces old . The substitute modifier substitutes the first occurrence of old with new . Placing a g before the s (as in gs/old/new/) causes a global substitution, replacing all occurrences of old . The / is the delimiter in the examples, but you can use any character that is not in either old or new . The final delimiter is optional if a RETURN would immediately follow it. As with the vim Substitute command, the history mechanism replaces an ampersand (&) in new with old . The shell replaces a null old string (s//new/) with the previous old string or string within a command that you searched for with ?string?.

An abbreviated form of the substitute modifier is quick substitution. Use it to reexecute the most recent event while changing some of the event text. The quick substitution character is the caret (^). For example, the command

$ ^old^new^ 

produces the same results as

$ !!:s/old/new/ 

Thus substituting cat for car in the previous event could have been entered as

$ ^car^cat
cat /home/jenny/memo.0507 /home/alex/letter.0507
...

You can omit the final caret if it would be followed immediately by a RETURN. As with other command line substitutions, the shell displays the command line as it appears after the substitution.

Modifiers (other than the substitute modifier) perform simple edits on the part of the event that has been selected by the event designator and the optional word designators. You can use multiple modifiers, each preceded by a colon (:).

The following series of commands uses ls to list the name of a file, repeats the command without executing it (p modifier), and repeats the last command, removing the last part of the pathname (h modifier), again without executing it:

$ ls /etc/sysconfig/harddisks
/etc/sysconfig/harddisks
$ !!:p
ls /etc/sysconfig/harddisks
$ !!:h:p
ls /etc/sysconfig
$

This table lists event modifiers other than the substitute modifier.

Modifiers

Modifier Function
e (extension) Removes all but the filename extension
h (head) Removes the last part of a pathname
p (print-not) Displays the command, but does not execute it
q (quote) Quotes the substitution to prevent further substitutions on it
r (root) Removes the filename extension
t (tail) Removes all elements of a pathname except the last
x Like q but quotes each word in the substitution individually
<< prev (1/2)

[Mar 30, 2008] comp.unix.shell Re bash history customizing

Date: Fri, 28 May 2004 13:55:36 +0000
2004-05-28, 14:12(+02), Gabkin: 
[...]
> As for zsh, I'm a bash man! (but does zsh have an evolutionary history
> facility like this?)

Yes, and much more. Some customizations possible for history:

APPEND_HISTORY <D>
If this is set, zsh sessions will append their history list to the
history file, rather than overwrite it. Thus, multiple parallel
zsh sessions will all have their history lists added to the
history file, in the order they are killed.

EXTENDED_HISTORY <C>
Save each command's beginning timestamp (in seconds since the
epoch) and the duration (in seconds) to the history file. The
format of this prefixed data is:

`:<BEGINNING TIME>:<ELAPSED SECONDS>:<COMMAND>'.

HIST_ALLOW_CLOBBER
Add `|' to output redirections in the history. This allows history
references to clobber files even when CLOBBER is unset.

HIST_BEEP <D>
Beep when an attempt is made to access a history entry which isn't
there.

HIST_EXPIRE_DUPS_FIRST
If the internal history needs to be trimmed to add the current
command line, setting this option will cause the oldest history
event that has a duplicate to be lost before losing a unique event
from the list. You should be sure to set the value of HISTSIZE to
a larger number than SAVEHIST in order to give you some room for
the duplicated events, otherwise this option will behave just like
HIST_IGNORE_ALL_DUPS once the history fills up with unique events.

HIST_FIND_NO_DUPS
When searching for history entries in the line editor, do not
display duplicates of a line previously found, even if the
duplicates are not contiguous.

HIST_IGNORE_ALL_DUPS
If a new command line being added to the history list duplicates an
older one, the older command is removed from the list (even if it
is not the previous event).

HIST_IGNORE_DUPS (-h)
Do not enter command lines into the history list if they are
duplicates of the previous event.

HIST_IGNORE_SPACE (-g)
Remove command lines from the history list when the first
character on the line is a space, or when one of the expanded
aliases contains a leading space. 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 make it vanish right away without entering another
command, type a space and press return.

HIST_NO_FUNCTIONS
Remove function definitions from the history list. Note that the
function lingers in the internal history until the next command is
entered before it vanishes, allowing you to briefly reuse or edit
the definition.

HIST_NO_STORE
Remove the history (fc -l) command from the history list when
invoked. 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.

HIST_REDUCE_BLANKS
Remove superfluous blanks from each command line being added to
the history list.

HIST_SAVE_NO_DUPS
When writing out the history file, older commands that duplicate
newer ones are omitted.

HIST_VERIFY
Whenever the user enters a line with history expansion, don't
execute the line directly; instead, perform history expansion and
reload the line into the editing buffer.

INC_APPEND_HISTORY
This options works like APPEND_HISTORY except that new history
lines are added to the $HISTFILE incrementally (as soon as they are
entered), rather than waiting until the shell is killed. The file
is periodically trimmed to the number of lines specified by
$SAVEHIST, but can exceed this value between trimmings.

SHARE_HISTORY <K>
This option both imports new commands from the history file, and
also causes your typed commands to be appended to the history file
(the latter is like specifying INC_APPEND_HISTORY). The history
lines are also output with timestamps ala EXTENDED_HISTORY (which
makes it easier to find the spot where we left off reading the
file after it gets re-written).

By default, history movement commands visit the imported lines as
well as the local lines, but you can toggle this on and off with
the set-local-history zle binding. It is also possible to create
a zle widget that will make some commands ignore imported
commands, and some include them.

If you find that you want more control over when commands get
imported, you may wish to turn SHARE_HISTORY off,
INC_APPEND_HISTORY on, and then manually import commands whenever
you need them using `fc -RI'.

I have:
HISTFILE=~/.zsh-history.$ZSH_VERSION
HISTSIZE=500
SAVEHIST=50000
setopt HIST_IGNORE_ALL_DUPS HIST_REDUCE_BLANKS \
HIST_SAVE_NO_DUPS HIST_VERIFY INC_APPEND_HISTORY NO_BANG_HIST

-- Stephane

[Mar 30, 2008] Bash Reference Manual - Bash History Builtins

Bash provides two builtin commands that allow you to manipulate the history list and history file.

fc
fc [-e ename] [-nlr] [first] [last]
fc -s [pat=rep] [command]
Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If the `-l' flag is given, the commands are listed on standard output. The `-n' flag suppresses the command numbers when listing. The `-r' flag reverses the order of the listing. Otherwise, the editor given by ename is invoked on a file containing those commands. If ename is not given, the value of the following variable expansion is used: ${FCEDIT:-${EDITOR:-vi}}. This says to use the value of the FCEDIT variable if set, or the value of the EDITOR variable if that is set, or vi if neither is set. When editing is complete, the edited commands are echoed and executed. In the second form, command is re-executed after each instance of pat in the selected command is replaced by rep. A useful alias to use with the fc command is r='fc -s', so that typing `r cc' runs the last command beginning with cc and typing `r' re-executes the last command (see section Aliases).
history
history [-c] [n]
history [-anrw] [filename]
history -ps arg
Display the history list with line numbers. Lines prefixed with with a `*' have been modified. An argument of n says to list only the last n lines. Options, if supplied, have the following meanings:
-w
Write out the current history to the history file.
-r
Read the current history file and append its contents to the history list.
-a
Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.
-n
Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.
-c
Clear the history list. This may be combined with the other options to replace the history list completely.
-s
The args are added to the end of the history list as a single entry.
-p
Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.
When the `-w', `-r', `-a', or `-n' option is used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE variable is used.

[Mar 30, 2008] Using Bash's History Effectively

vague memory

... ... ...

Here's an example that suppresses duplicate commands, the simple invocation of ls without any arguments, and the shell built-ins bg, fg, and exit:

[Dec 11, 2007] What's in your bash History -

O'Reilly ONLamp Blog

If you’re like me, you never want to lose a command. I’m constantly searching back through them to find out just what those command line flags were, what the esoteric command is (and where it’s located), and most of all: what in Tcl’s name did I do last month when I installed foobazzulator. First thing to know: control-r.

However, bash ships with a ridiculously short memory: 500 commands. That’s not enough: right now, my powerbook’s iTerm prompt is:

[/Users/jwellons]
111280$

Go into your .bash_profile now and add these lines, before you read on:

HISTFILESIZE=1000000000
HISTSIZE=1000000

Unless you’re very precocious, that should square you away for about 10 years (I’m at about 22 months since enlightenment).

Once you develop control-r as a fast way to search your command history, you’ll notice immediate stress relief, and as I do everyday, wish you’d done this years before.

Now that you have commands piling up, you can do more than just use them. You can analyze them too, for fun and profit (since time = money)!

>

The basic starter is

cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -n 30

In other words, just what do you spend so much time typing? Here’s the money: Anything in at least the top ten, assuming it is not from somHistory can be browsed using arrow commands, but more efficient way is to use Ctrl-R for anything but previous half-dozen of commands. Ctrl-R mechanism provides search in history: typed characters can be in any place of the command will autocomplete as soon as there’s a match to a history entry, then you just need to hit enter to reexecute the command -time splurge, needs to be aliased to a one or two letter shorthand.

My top command is v for vim, which I’ve executed 9225 times. If those two extra letters, which require me to switch hands, then use adjacent fingers take an extra half-second each time: that’s well over an hour wasted! And what a terribly drudgerous and unhealthy hour it is too: mindlessly typing im 9225 times!

Even worse, who knows how desperately I needed to open that file, and yet I needed to type superfluous characters, probably messing them up on the first three tries.

Actually, that’s only my second highest command. cl tops the list at 11116. You see, last time I did this analysis, I noticed I was spending (yes, spent, like lines of code, there is no glory in using a lot of them) many commands first cd‘ing to a directory, then listing the contents. Here’s cl:

# Compress the cd, ls -l series of commands.
alias lc="cl"
function cl () {
if [ $# = 0 ]; then
cd && ll
else
cd "$*" && ll
fi
}

There you have it, accompanied by its most common typo. Add this to your bash profile now, and you’ll add several hours to your life. Hurry! Don’t wait! It may not seem like much now, but no one lies on her deathbed wishing she had spent more time first changing directories, waiting for it to return, then typing ls.

Since I know you’re dying to see the rest, here’s my whole thirty, unaliased for readability:

11116 cl
9225 vim
7833 ll
The alias of ll depends on the OS I’m on, but for my Mac, it’s ls -AGlFT.
5145 cd
4858 clear
4563 rm
3950
alias to log into the master db server at my old workplace
3740 lt
version of ll that puts most recently modified stuff at the bottom. Mandatory! Add -tr to ll above
3435 mod
a function that does lt, but runs it through tail
3236 mv
2103 ls
1887 grep
1863 perl
1834 df
1767 mzscheme
1679
alias to log into my personal server
1580 g++
1544 cat
1186 scp
1080 find
988 man
925 echo
921 mkdir
907 sudo
888 history
841
alias to log into the dev server at my old workplace
831 cp
763 gpg
758 locate
733 gzip

Depending on response, we may do another section with pie charts, finding all the typo permutations for simple commands (there’s a lot), and searching for common sequences.

What I really want to see, though, is your top commands.

[Sep 3, 2007] Linux command line tips history and HISTIGNORE in Bash

2007/09/03 | Builder AU

The amount of commands stored in the history file is dependent on the size of the file you allocate. By default 500 commands are stored, but you can tweak these values by setting the following options in a startup file:

HISTSIZE = 1000
HISTFILE = ~/.bash_history

To print out the entire history file, you can use the history command:

$ history | tail -10
  522  gvim
  523  vim .bashrc
  524  vim .bash_profile
  525  history
  526  ls
  527  echo $SHELL
  528  echo $HISTFILE
  529  echo $HISTSIZE
  530  vim .bashrc
  531  history | tail -10
$

The number in the left column is the history item number. This can be bigger than the size of your stored history file, as commands from the current shell are not added to the file until the shell is closed.

You can jump straight to executing any line in your history by typing an '!' followed by the item number. So for example if we wanted to echo the shell name to the console again we could write echo $SHELL or simply:

$ !527
/bin/bash
$

Searching command history

You can also use the ! command to search through the history, by replacing the item number with characters from the start of the command. The previous example can now just as easily be written:

$ !echo
echo $SHELL
/bin/bash
$

Often this becomes unwieldy since you have to match the command from the beginning, and many commands start in the same way (eg. "echo $HISTFILE", "echo $HISTSIZE", "echo $SHELL"). You can always hack something together using history and grep, but there's an easier way with the '!?' command. '!?' followed by a simple string, executes the last command in the history that contains that string.

So, to reprint the name of the shell we could have simply typed:

$ !? SHELL
/bin/bash
#

More usefully, you can interactively search the command history at the console by hitting CTRL-R at the prompt and typing the section of the string you are looking for. You can only use CTRL-R interactively, that is, not in a script (although relying on the history in a script can be dangerous anyway), but it can save you more keystrokes as it displays the match at each point, meaning you can stop searching once you've found it.

[Dec 29, 2006] Sys Admin v15, i04 Archiving Korn Shell History Files by John Spurgeon and Ed Schaefer

April, 2006 (samag.com) When used as an audit utility, shell history has serious drawbacks. For example:

[Dec 29, 2006] Shell game Managing Bash command history by James Turnbull

09.27.2006 | Techtarget.com

The Bash shell is the default shell environment in most Linux distributions, including all flavours of Red Hat. One default feature of the Bash shell is to record a history of all the commands entered by a user in a log file called .bash_history, found in the user's home directory.

The history in this file can be retrieved from the command line using the more or cat binaries or by using the internal Bash command, history. For many users, it is useful to retrieve the previously executed commands, usually to save the effort of re-typing them.

So why might we want to limit or disable this Bash command history? Well, among the commands that your users type are file names, command names, IP addresses, passwords and a myriad of other data that are potentially valuable to an attacker intent on subverting or compromising your host. Keeping a record of this data means an attacker may only need to compromise an individual .bash_history file rather than a more difficult source of data.

To limit the size and behaviour of the .bash_history file, you need to edit the behavior of the shell in the /etc/profile file (the central version of the .bash_profile file usually contained in users home directories). Add or change the following three lines to the file:

export HISTSIZE=100
export HISTFILESIZE=100
unset HISTFILE

The first two lines set the length and size of the history stored in the file. The last line unsets the HISTFILE variable that tells the Bash shell not to save history when the user logs out of the interactive session. This means an online user will only be able to see the history of the last 100 commands, and that history will disappear after the user logs out of the host.

A further way to ensure the command history is removed is to include a command in the .bash_logout file (other shells use the .logout file). The contents of the .bash_logout file are executed when the user logs out. You can see a simple .bash_logout file on the following lines:

# ~/.bash_logout

/bin/rm -f $HOME/.bash_history
clear

The rm command will remove the .bash_history file from the users home directory when the user logs out. To add this to the file you can edit the .bash_logout file contained in the /etc/skel directory (the contents of this directory are copied to the home directories of all new users when they are created). Existing users will need the .bash_logout files in their home directories adjusted to add the command.

There is, however, one gotcha associated with this change, and that is that if an attacker has compromised your host, then the history of their actions might not be recorded. Of course, any talented attacker will use other means (including this exact method and others like the deletion or editing of log files) to remove that history.

[Dec 29, 2006] CLI Magic Bash history expansion

Linux.com

Entering the history command without any switches displays the history list with line numbers. If that's too much information, enter history N where N is the number of previously executed commands you want displayed.

Bash also allows for incremental search on the history list. Use ctrl+r for a backward/reverse search or ctrl+s to perform a forward search. As you type the first few letters of the command, the last command from the history list that matches with your string would be displayed.

Since you are at the command line, you can club combine some popular commands and cook up interesting surprises. One such example is to pass history through grep along with the first few letters of a command. For example, try this simple hack, which displays all the commands beginning with the letters you supply:

history | grep -i first-few-letters-of-command

History expansion

History expansion serves two purposes, the first is repeating a command from the history list. The second is selecting portions from a command for substituting into the current command.

History expansions are implemented by the history expansion character '!'. The line selected from the history list is called 'event' and portions of that command that are selected are called 'words'. Depending on your needs, you can either use 'word' or 'event designators'.

Event designators

Event designators, invoked by the history expansion character are used for command substitution, that is, repeating a command from the list. ! starts history substitution. It can be escaped with a backslash (\), since it is a special character. In this example, history command 1008 -- a simple ls -- is repeated:

!1008
ls
animate
Desktop
DOWNLOADS
evolution
songs
sonu_result.png
star trek

Another useful event designator is !string, which runs the most recent command beginning with the string you specify.

Word designators

Word designators are used to select specified words from an event. A colon (:) is used to separate word designators from events. Words in a command are numbered from the beginning of the line, with the first word denoted by zero (0). Once selected, a word is inserted into the current command separated by a single space.

Some common word designators are n, which refers to the nth word; ^ refers to the first word; $ refers to the last word and * refers to the complete line entry except for the first word, which is the command.

For example, !conv:5 displays the fifth word from the last command beginning with !conv, while !conv:$ would display the last word from the same command. Please note that !conv:* is same as !conv:1-$ - both resulting in the complete command except for the first word.

Modifying selected words

There are a number of modifiers that help you to describe how you want the selections to behave. Each modifier in a command needs to be preceeded with a colon (:) and you can use more than one modifier in a command. A very popular modifier is p which results in the command being listed instead of being executed. For example:

!l:p
ls -l

There is also a search-and-replace type modifier that you can use when you wish to replace some text with some other. This modifier is s/old/new. For example:

!!:s/-l/-a/
ls -a
[...]

Note: A !! is used to refer to the last command in the history list. The last command was ls -l and so the -l was replaced with -a and the new command, ls -a was executed. 

Conclusion

After encountering a thread in a popular forum board that just brushed on Bash and its event designators, I've since done enough reading from the man pages and the web to officially declare that it is a very cool feature which requires very little effort to get hooked on.

[Dec 29, 2006] Tips&Tricks Bash tips and tricks

Some bash-history tips

MandrakeUsers.org

The usage of the bash-history feature could be as fun as helpful, also it can save a lot of time if you are one of those who use bash-terminals for everything (if you are not, what the hell are you doing here with Linux Mr. Green ...just kidding Wink )

I'm not going to talk in extent about the usage of bash-history, I only want to show you a couple of tips. If you want to learn more about it you can take a look to the chapter 7 of the Bash Reference Manual (such as ramfree "pointed" long time ago, should be under every linux user's pillow Wink )

Here are the tips:

$HISTIGNORE or How-To improve the history usage:

You might want to set up the shell variable $HISTIGNORE to avoid having consecutive duplicate commands or unuseful commands appended to the history list. So you'll get rid of the never ending story of hitting the up arrow trough simple and repetitive commands.

Here is an example of usage:

export HISTIGNORE="&:l[sl]:[fb]g:exit"

Setting this way the $HISTIGNORE, the consecutive duplicated commands, the simple calls to ls, ll, fg and bg commands without arguments, and calls to the exit command will not be appended to the history list, so navigate though it will be much easier.

[Dec 29, 2006] BASH HISTORY SEARCH AND REPLACE

Mark Stosberg
# BASH HISTORY SEARCH AND REPLACE
      ^string1^string2^ Quick substitution. Repeat the last command, replacing string1 with
      string2. Equivalent to ``!!:s/string1/string2/''

# MORE BASH COMMAND LINE TRICKERY (messing with the history)
make a directory then move into it:
mkdir cgi-bin; cd !#$
!#$ is shorthand for "the first word of this command". If I wanted to pick the third word
out of the previous command, that would be: !!:3 (don't forget there is a zeroth word).

# execute the most recent command the contains the following string:
!?string

# globally search replace on the previous line:
!!:gs/old/new/

# HEADS AND TAILS
ever wanted to copy a few files in the same directory, all of which have same
long prefix, like: cp /usr/local/etc/apache/file1.txt /usr/local/etc/apache/file2.txt
you can grap and reuse that prefix. It's named !#:1:h like:
cp /usr/local/etc/apache/file1.txt !#:1:h/file2.txt !#:1:h/file3.txt


# SAVING A COMMAND WITHOUT EXECUTING
While using bash, if you have typed a long command, and then realize you don't want to execute it yet, don't delete it. Simply
append a # to the beginning of the line, and then hit enter. Bash will not execute the command, but will store it in history so later
you can go back, remove the # from the front, and execute it.

# GOING FORWARD A BACK A WORD
META-F goes forward a word
META-B goes backward a word

# COMPARE A FILE WITH IT'S VARIOUS VERSIONS IN THE SAME DIRECTORY
diff file.*

# EXAMPLE of a basic command line script
% for f in `ls | grep -v ".sh"`; { mv ${f} ${f}.sh }

# searching the history
Cntrl-R starts a reverse incremental search

# Keyboard shortcuts:
CTRL-k: delete ('kill') from cursor position to the end of the line
*	CTRL-u: delete from cursor position to the beginning of the line
*	ALT-d: delete from cursor position to the end of the current 'word'
*	CTRL-w: delete from cursor position to the beginning of the current 'word'
*	CTRL-a: move cursor to the first character of the line
*	CTRL-e: move cursor beyond the last character of the line
*	ALT-a: move cursor to the first character of the current 'word'
*	ALT-e: move cursor to the last character of the current 'word'
*	CTRL-y: insert latest deleted 'word'
*	ESC-_ or !$: repeats the last argument of the previous command.
Example: You created a directory with mkdir /usr/local/bin/peter/pan. Now you want to change into that directory with cd. Instead of typing the path again, you type cd ESC-_ or

# Diff 2 files with a common base:
diff {alpha,beta}site/config/Config.pl

[May 1, 2002] gh - grep history

#!/bin/bash
#
# gh - grep history
#
# A simple script for the forgetful and lazy (I use it all the time).
#
# This script allows you to re-use bash history commands...
#
# A: ...if you can't remember the whole command or path...
# B: ...if you don't feel like pressing the "up" arrow for 2 minutes...
# C: ...if you are too forgetful or lazy to type "grep <search-string> ~/.bash_history"!
#
# -Mike Putnam   http://www.theputnams.net
#
# Usage: gh <search-string>
#

grep $1 ~/.bash_history

exit 0

[May 5, 2001] Page-up-Page-down keys and shell history

On Sat, 5 May 2001, Joost van der Lugt wrote:

Hi all,

I've been looking into this for a litle bit and am unable to find an answer. This is a feature AFAIK only implemented by SuSE:

At a prompt type one or more characters then use Page Up/Page Down to scroll through all commands that started with these characters.
So typing:

cd

would then give a 'scrollable' list of anything that started with cd simply hit enter when you reach the command you were looking for.

I know Ctrl-r and am familiar with ! but they are not as nice, and really not the same. Would anybody have a clue as to how this could be implemented?

I don't think it is shell dependent, so I don't think it's a bash option but I could very well be wrong.
(Tried the dotfile generator, but don't see anything there)

The option is _really_ addictive therefore my post:)
Thanks,

--
Cheers,

Joost van der Lugt
--
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org>

Recommended Links

Softpanorama Top Visited

Softpanorama Recommended

Intermediate UNIX Tutorial Notes

A history substitution begins with a !, and may occur anywhere on the command line. The power of history substitution is impressive. There are too many ways of using it to explain here, but complete details can be found in the csh man page. Some of the simpler forms are:

	!!      Refer to the previous command
	!n      Refer to command-line n
	!-n     Refer to the current command-line minus n.
	!str    Refer to the most recent command starting with str.
	!?str   Refer to the most recent command containing str. 

You can also refer to individual words of a previous command. If necessary, you can separate the event specification (above) from the word specification.

	!!:0    The first word of the previous command line
	!!:n    The nth argument of the previous command
	!!:$    The last argument of the previous command
	!!:*    All of the arguments of the previous command

Further modification to the commands can be done including editing particular words, but we won't go into further detail here. Suffice it to say that there's probably a way to do whatever it is you are trying to accomplish with history substitution.

Quick substitution can be done, if all you want to do is change a single string from the previous command. For instance, if you just sent mail to dela with "mail dela", and now want to send mail to kaser, you can say

^dela^kaser^

and the string "dela" will be replaced with "kaser".

The History Mechanism

bash stores the commands you type and provides access to them through its history mechanism. This makes it possible to repeat a previous command, include portions of a previous command in the current command or re-execute a previous command after fixing a typing error.

The list of all commands currently saved by the history mechanism is displayed by executing the history  command. The history  commands numbers the old commands it displays. Typing

!n
will re-execute the command numbered n.

If you would rather not lookup the number of a previous command, you can type !  followed by the first characters of the command. For example,

!ls
will execute the last command that began with ls.

Another way to avoid looking up the number is to use control-p. Pressing control-p  will display your previous command, which you can then edit. Repeatedly pressing control-p  will go back to earlier commands.

If the command you want to execute is similar to a previous command, or you want to fix a typing mistake, you can control-p  back to the command, then edit it using control-b  or the left arrow key to move the cursor back a character, contol-f  or the right arrow key to move forward a character, control-a  to move to the beginning of a line, control-e  to move to the end of a line, and control-d  to delete a character. Backspace  and Delete  work as usual. Anything you type is inserted where the cursor is located.

Another useful function the history mechanism provides is the ability to re-use the arguments of the previous command. The characters !*  occurring in a command are replaced by the arguments to the previous command. Thus, one can first check the contents of a file by typing

less somelongfilename
and then print the file by typing
lp !*

You can avoid typing somelongfilename by just typing enough of it so that the shell can recognize it, (for example ``somel'' for the filename ``somelongfilename'') and pressing tab. bash will type the rest of the filename for you. If there is another file of a similar name, such as ``somelongfile2'', and you use tab to complete the name, bash will type the beginning of the two filenames until it encounters a discrepancy. In this case, it would type ``somelongfile''. Then it will beep and wait for you to finish typing in the filename of your choice.

Finally, you can fix simple typing mistakes in a command by typing

^old-string^new-string
This causes the shell to re-execute the last command after first replacing the first occurrence of old-string by new-string.

Reference

Bash Reference Manual Word Designators

Word designators are used to select desired words from the event. A `:' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^', `$', `*', `-', or `%'. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

For example,

!! 
designates the preceding command. When you type this, the preceding command is repeated in toto.
!!:$ 
designates the last argument of the preceding command. This may be shortened to !$.
!fi:2 
designates the second argument of the most recent command starting with the letters fi.

Here are the word designators:

0 (zero) 
The 0th word. For many applications, this is the command word.
n 
The nth word.
^ 
The first argument; that is, word 1.
$ 
The last argument.
% 
The word matched by the most recent `?string?' search.
x-y 
A range of words; `-y' abbreviates `0-y'.
* 
All of the words, except the 0th. This is a synonym for `1-$'. It is not an error to use `*' if there is just one word in the event; the empty string is returned in that case.
x* 
Abbreviates `x-$'
x- 
Abbreviates `x-$' like `x*', but omits the last word.

If a word designator is supplied without an event specification, the previous command is used as the event.

bash reference card

History expansion is similar to csh ’s. It is enabled by default in interactive shells. History expansion

happens before the shell breaks the input into words, although quoting is recognized and quoted text is

treated as one history ‘‘word’’. History substitution is performed on history events ,  which consist of an event designator (which previous line to start with), a word designator (which word from that line to use, starting with zero), and one or more optional modifiers (which parts of the words to use).

Colons separate the three parts, although the colon between the event designator and word designator may be omitted when the word designator begins with ˆ , $ , * , , or % . Each modifier is separated from the nextone with a colon. The histchars variable specifies the start-of-history and quick substitution characters, and also the comment character that indicates that the rest of a line is a comment. The previous command is the default event if no event designator is supplied.

The event designators are:

The modifiers are:

Bash Reference Manual - Bash History Builtins

Bash provides two builtin commands that allow you to manipulate the history list and history file.

fc 
fc [-e ename] [-nlr] [first] [last]
fc -s [pat=rep] [command]
Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If the `-l' flag is given, the commands are listed on standard output. The `-n' flag suppresses the command numbers when listing. The `-r' flag reverses the order of the listing. Otherwise, the editor given by ename is invoked on a file containing those commands. If ename is not given, the value of the following variable expansion is used: ${FCEDIT:-${EDITOR:-vi}}. This says to use the value of the FCEDIT  variable if set, or the value of the EDITOR  variable if that is set, or vi  if neither is set. When editing is complete, the edited commands are echoed and executed. In the second form, command is re-executed after each instance of pat in the selected command is replaced by rep. A useful alias to use with the fc  command is r='fc -s', so that typing `r cc' runs the last command beginning with cc  and typing `r' re-executes the last command (see section Aliases).
history 
history [-c] [n]
history [-anrw] [filename]
history -ps arg
Display the history list with line numbers. Lines prefixed with with a `*' have been modified. An argument of n says to list only the last n lines. Options, if supplied, have the following meanings:
-w 
Write out the current history to the history file.
-r 
Read the current history file and append its contents to the history list.
-a 
Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.
-n 
Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.
-c 
Clear the history list. This may be combined with the other options to replace the history list completely.
-s 
The args are added to the end of the history list as a single entry.
-p 
Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.
When the `-w', `-r', `-a', or `-n' option is used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE  variable is used.

Bash Reference Manual Word Designators

Word designators are used to select desired words from the event. A `:' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^', `$', `*', `-', or `%'. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

For example,

!! 
designates the preceding command. When you type this, the preceding command is repeated in toto.
!!:$ 
designates the last argument of the preceding command. This may be shortened to !$.
!fi:2 
designates the second argument of the most recent command starting with the letters fi.

Here are the word designators:

0 (zero) 
The 0th word. For many applications, this is the command word.
n 
The nth word.
^ 
The first argument; that is, word 1.
$ 
The last argument.
% 
The word matched by the most recent `?string?' search.
x-y 
A range of words; `-y' abbreviates `0-y'.
* 
All of the words, except the 0th. This is a synonym for `1-$'. It is not an error to use `*' if there is just one word in the event; the empty string is returned in that case.
x* 
Abbreviates `x-$'
x- 
Abbreviates `x-$' like `x*', but omits the last word.

If a word designator is supplied without an event specification, the previous command is used as the event.

Bash Reference Manual Commands For Manipulating The History

accept-line (Newline or Return) 
Accept the line regardless of where the cursor is. If this line is non-empty, add it to the history list according to the setting of the HISTCONTROL  and HISTIGNORE  variables. If this line is a modified history line, then restore the history line to its original state.
 
previous-history (C-p) 
Move `back' through the history list, fetching the previous command.
next-history (C-n) 
Move `forward' through the history list, fetching the next command.
beginning-of-history (M-<) 
Move to the first line in the history.
end-of-history (M->) 
Move to the end of the input history, i.e., the line currently being entered.
reverse-search-history (C-r) 
Search backward starting at the current line and moving `up' through the history as necessary. This is an incremental search.
forward-search-history (C-s) 
Search forward starting at the current line and moving `down' through the the history as necessary. This is an incremental search.
non-incremental-reverse-search-history (M-p) 
Search backward starting at the current line and moving `up' through the history as necessary using a non-incremental search for a string supplied by the user.
non-incremental-forward-search-history (M-n) 
Search forward starting at the current line and moving `down' through the the history as necessary using a non-incremental search for a string supplied by the user.
history-search-forward () 
Search forward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.
history-search-backward () 
Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search. By default, this command is unbound.
yank-nth-arg (M-C-y) 
Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command.
yank-last-arg (M-. or M-_) 
Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg  move back through the history list, inserting the last argument of each line in turn.

HISTCONTROL

HISTCONTROL  A value of 'ignorespace' means to not enter lines which begin with a space or tab into the history list. A value of 'ignoredups' means to not enter lines which match the last entered line. A value of 'ignoreboth' combines the two options. Unset, or set to any other value than those above, means to save all lines on the history list. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.

 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. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound
command are not tested, and are added to the history regardless of the value of HISTCONTROL.

In bash 3.0 HISTCONTROL can be set to `erasedups' option, which causes all lines  matching a line being added to be removed from the history list.

     export HISTCONTROL=erasedups

HISTIGNORE

A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the beginning of the line and must match the complete line (no implicit '*' is appended). Each pattern is tested against the line after the checks specified by HISTCONTROL  are applied. In addition to the normal shell pattern matching characters, '&' matches the previous history line. '&' may be escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTIGNORE. HISTIGNORE  subsumes the function of HISTCONTROL. A pattern of '&' is identical to ignoredups, and a pattern of '[ ]*' is identical to ignorespace. Combining these two patterns, separating them with a colon, provides the functionality of ignoreboth.

You might want also to set up the shell variable HISTIGNORE to avoid useless command littering you history. Natural candidates are cd -, all variants of ls without argument, ll, etc.

     export HISTIGNORE="&:l[sl]:[fb]g:cd \-"

Setting this way the $HISTIGNORE, the consecutive duplicated commands, the simple calls to ls, ll, fg and bg commands without arguments, and calls to the exit command will not be appended to the history list, so navigate though it will be much easier.

Event Designators

An event designator references a command line stored in the history file. There are several formats of event designators, they are as follows.

! Start a history substitution command, except when followed by a space, tab, new-line, =, or (.
!! Reference the previous command line. Typing !! and pressing Return executes the previous command.
!n Reference command line with event number n from the history file. This references a specific event number. For example, if the current event number is 7, you can specify an event number from 1 to 6. The command !3 would reexecute event 3, vi wsr.092989.
!-n Reference the nth previous command event. The current event number -n. For example, if the current event number is 7 and you type !-3, event number 4 is executed.
!str Reference the most recent command line starting with string str. For example, !vi reexecutes the "vi apiprog.c" command.
!?str[?] Reference the most recent command line containing the string str. For example, !?dly reexecutes the "find ..." command.
!{str} Insulate a reference string from adjacent characters.

Word Designators

Word designators provide a way to reference specific arguments (words) from a command line stored in the history file. A colon (:) separates the event designator and the word designator. If the word designator begins with $, %, ^, *, or - the : is not needed. If you want the word to be selected from the previous command line, the second ! is not required. For example, !!:2 and !:2 both refer to the second word on the previous command. Words are zero relative, that is, word zero is the command itself. The first argument (arg1) on the command line is word 1.

The available word designators are as follows:

Modifiers

You can add modifiers after the word designators. A modifier is used to modify the referenced command line. Each modifier must be preceded with a colon (:).

It make sence to try command with modifier :p in case the connad is destructive (like rm and rmdir commands are) Suffix :p allows to print a command, but not execute it. Another three useful modifers are:

Let's say I'm reading a file nested deeply in a directory structure. When I finish editing the file, I realize that there are some other operations I want to do in that directory and that they would be more easily accomplished if I were in that directory. I can use :h to help get me there.

links /usr/local/share/doc/3dm/3DM_help.htm
cd !$:h
links !-2 $:t

As we see !$ can be modified by :h.

In our next example, we've downloaded a tarball from the Internet create directory with the name of the tarball and unpack the tarball into it. We check

wget http://www.example.com/path/to/test.tgz
mkdir !$:r
cd !$
tar xzvf ../test.tgz

The second command will create a directory called 'test' in the current directory.

Word modifiers can be stacked as well. Here is more complex example, in which we'll first download a file to /tmp, and then create a directory for the contents of that tar file in /usr/local/src and then unpack the tarball into it:

cd /tmp
wget http://www.example.com/path/KickassApplicationSuite.tar.gz
cd /usr/local/src/
mkdir !-2$:t:r:r
{creates directory called 'KickassApplicationSuite'}
cd !$
tar xvzf /tmp/!-4$:t

The first three commands are fairly common and use no substitution. The fourth command, however, seems like gibberish. We know !-2 means the command prior to the most recent one and that $ indicates the last argument of that command. We even know that :t will strip off the path portion of that argument (in this case, even the "http://".) We even know that :r will remove the file-extension to that argument, but here we call it twice, because there are two extensions (.gz is removed by the first :r and .tar is removed by the second.) We then cd into that directory (!$ again, is the argument to the previous command, in this case the argument to mkdir, which is 'KickassApplicationSuite'.) We then untar the file. !-4$ is the last argument to the command four commands ago, which is then modified by :t to remove the path, because we added the path as /tmp/. So the last command becomes tar xvzf /tmp/KickassApplicationSuite.tar.gz.

There's even a word modifier for substitution. :s can be used similarly to circumflex hats to do simple line substitution.

 vi /etc/X11/XF86config
!!:s/config/Config-4/

We know that !! means the previous command string. :s modifies the previous command, substituting the first argument to :s with the second argument to :s. My example used / to delimit the two arguments, but any non-whitespace character can be used. It's also important to note that, just like circumflex hat substitution, the substitution will only take place on the first instance of the string to be substituted. If you want to affect every instance of the substitution string, you must use the :g word modifier along with :s.

 mroe file1 ; mroe file2
!!:gs/mroe/more

The second command substitutes (:s) more for all (:g) instances of mroe. Hint: :g can be used with circumflex hats too!

The final word modifier we'll look at in this tutorial is &. & means repeat the previous substitution. Let's say we're examining file attributes with the ls command.

ls -lh myfile otherfile anotherfile
!!:s/myfile/myfile.old/

Seems simple enough. :s steps in and changes myfile to myfile.old so we end up with ls -lh myfile.old myfile2 myfile3. & is just a shortcut that we can use to represent the first argument to :s The following example is equivalent to the example above:

ls -lh myfile otherfile anotherfile
!!:s/myfile/&.old/

History Editing Examples

The following examples illustrate how to use the Event Designators, Word Designators, and the Modifiers.

Archiving Korn Shell History Files

Maybe you want to keep your shell history around for forensic analysis...

Sys Admin v15, i04 Archiving Korn Shell History Files

When used as an audit utility, shell history has serious drawbacks. For example:




Etc

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit exclusivly for research and educational purposes.   If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner. 

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no less then 90 days. Multiple types of probes increase this period.  

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2015 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: June 04, 2016