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.doesn't actually make sense:
!#
selects the line typed so far, but that'star zxvf
and we don't want to re-use any of that. It should bewhich selects the last portion of the previous line (
!$
), which is the URL given towget
, 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
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). Thechmod
andcd
commands proceed in the same way.