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.
parameter expansion using bang dollar (`!$`)
Asked Answered
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
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
© 2022 - 2024 — McMap. All rights reserved.
alt+.
more than!$
myself. Rather than expanding it later it'll (assuming you're in emacs mode) put the last argument of the previous command inline, fully expanded already. That wouldn't work if you're using other words than the last though, or going more commands back like!:-2
or!mv:1
– Chieflyalt+.
, but it is even faster than a parameter-expanded!$
would be for the proposed use. – Coastline