Bash Commands – Meaning of !#, !$:r:r, !#:t

bashcommand history

I have been trying to install a Nagios package and stumbled upon these weird bash expansions on a guide here. Can someone help me understand what do the expansions mean? A wiki link would also be helpful for future reference.

root@z:~> wget http://mathias-kettner.de/download/check_mk-1.1.7i5.tar.gz
root@z:~> tar zxvf !#:t
root@z:~> chown -R root:root !$:r:r && chmod -R o-w !$:r:r && cd !$:r:r
root@z:~/check_mk-1.1.7i5> ./setup.sh

Best Answer

These are bash history expansion keywords. They select a line from the shell history and reinsert (portions of) it, possible after modification. The site you linked to does a reasonable job explaining them, but it doesn't describe all of them.

tar zxvf !#:t

doesn't actually make sense: !# selects the line typed so far, but that's tar zxvf and we don't want to re-use any of that. It should be

tar zxvf !$:t

which selects the last portion of the previous line (!$), which is the URL given to wget, and takes the filename portion (:t, for tail — strictly speaking it's whatever's left after the last directory separator, /, which happens to work nicely with URLs).

Then

chown -R root:root !$:r:r

selects last portion of the previous line, drops the file extension (:r — strictly speaking, it removes whatever's after the last ., including the .) twice, which gives the directory name (assuming the tarball contains a directory with the same name as the tarball's base name). The chmod and cd commands proceed in the same way.