Bash Cleverness - Too Clever by 0.5?

06 Apr 2020

I recently saw !$:t in a tar command and had no idea what it was doing:

Bash
1
2
wget https://path/to/some/downloadable-file.tar.gz  
tar zxvf !$:t  

In line two, !$  selects the previous command, courtesy of command history expansion, and then :t  gets the final section of that command (so, in this case, we end up with downloadable-file.tar.gz).

The man page for the history command refers to :t as follows:

Remove all leading file name components, leaving the tail.

(Which reminds me of the basename and dirname commands.)

I saw the above example here - where additional dexterity is used.

I admire the conciseness, but would prefer explicitness, in this case.