Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parameter expansion using bang dollar (`!$`)

Is there any way to use !$ in a parameter expansion context? The desired usage that motivates this question is rapid (in terms of key strokes) alteration of the name of a file (e.g., instead of saving the file name in a variable and executing rsvg-convert $svg > ${svg/.svg/.png}, one could instead use rsvg-convert $! > $!{/.svg/.png}, where $!{/.svg/.png} is erroneous syntax intimating the desired effect; when the file in question was the last token on the preceding line, such a command can often be typed more quickly than alternatives like using tab completion in the presence of files sharing prefixes of varying length, or copying and pasting the file name by selecting with a mouse). As far as I can tell, there is no way to employ !$ in such a context, but perhaps through some chicanery a similar effect could be achieved.

like image 945
user001 Avatar asked Jun 11 '17 00:06

user001


2 Answers

Depending on how sophisticated you want the substitution, history expansion does support replacing the first occurrence of a string with another. You just precede the substitution with : like:

rsvg-convert !$ > !$:s/.svg/.png

You can see all the history modifiers here

At least in emacs-mode bash will also put the last argument of the previous command inline (not for expansion when you run the command) if you press alt+.. So in this case it might be fastest to type:

rsvg-convert

then alt+.>alt+. then delete the extension it just put in place with alt+bksp then the new extension: png

like image 103
Eric Renouf Avatar answered Oct 06 '22 10:10

Eric Renouf


If you look further into the modifiers in Eric's example, you could also do:

rsvg-convert !$ > !$:r.png

Assuming .svg is a suffix of course

like image 42
grail Avatar answered Oct 06 '22 08:10

grail