Bash history substitution trick

I saw the following neat little trick in this link:

# renaming a long file-name using a regex
$ touch config.txt
$ mv config.txt !#:1:s/txt/json

The exact meaning of the !# is, quoting from the man page:

Event Designators       An event designator is a reference to a command line entry in the history list.  Unless the reference is absolute, events are relative to the current position in the history list.
       !      Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). 

<snipped>

 !#     The entire command line typed so far.

From ‘man bash’

i.e. most people know that bash can insert things from history using commands like !! and !50 (i.e. rerun last command and rerun the 50th history item respectively).

But !# let’s you take something from the existing line and re-use it.

Here is another example, from here:

curl http://beyondgrep.com/ack-2.14-single-file > ~/bin/ack && chmod 0755 !#:3 

In this case, !#:3 takes the 3rd element from the existing command. Starting from zero, that turns out to be ~/bin/ack in this case.

Bash: Move between directories with ease

Bash has tons of stuff that can really make one productive on the terminal. If you just moved into a folder and want to quickly switch back to the one you came from, cd - does the job.

Here’s an example:

thaum:/etc/apache2# cd /var/log/apache2/
thaum:/var/log/apache2#
thaum:/var/log/apache2# # do something
thaum:/var/log/apache2# cd – /etc/apache2
thaum:/etc/apache2#

That works out for simple tasks, but there’s a lot more. Say hello to pushd. Bash has a directory stack that holds your current working directory. Call pushd with another path and it gets added to the stack. Repeat as many times as needed and you get a pile of directories that you can switch to immediately. popd removes the current entry from the stack.

This is how it works:


thaum:/etc/apache2# ### I want to switch between my logs and my etc now, so I start with this
thaum:/etc/apache2# pushd /var/log
/var/log /etc/apache2
thaum:/var/log# cd apache2
thaum:/var/log/apache2# ### I can go to other folders now and the stack is updated accordingly
thaum:/var/log/apache2# # do something
thaum:/var/log/apache2#
thaum:/var/log/apache2# pushd /var/www ### Add my apache root folder to the stack as well
/var/www /var/log/apache2 /etc/apache2 ### Three entries are now available!
thaum:/var/www#
thaum:/var/www# # do something else
thaum:/var/www#
thaum:/var/www# pushd +2 ### I want to go back to my /etc folder, so I call pushd with the +2 arg
/etc/apache2 /var/www /var/log/apache2
thaum:/etc/apache2#
thaum:/etc/apache2# pushd ### Without any args, it goes to the most recent directory in the stack
/var/www /etc/apache2 /var/log/apache2
thaum:/var/www#

At work I organize something like this for a set of commonly used folders in a screen session, and it really helps. It’s a bit of a bother typing pushd +2 or pushd +3 each time, so I also have the following aliases to really speed things up.


function pd { pushd $@; }
function pd2 { pushd +2 $@; }
function pd3 { pushd +3 $@; }

Now I just do a pd /path/to/file and pd2, pd3 etc to swap between them.