I would like to know how/if I can reuse a command from my terminal history, but in a modified version. Here’s an example:
$ filter_script file2 > output_file2
$ # ...
# now run the same command, but replace '2' with '4'
$ filter_script file4 > output_file4
This is a very simple example, and of course I can simply access the command from the history and manually replace the two 2s, but is there a more elegant way?
Thanks a lot for your time!
If there’s only one instance of whatever it is you want replaced,
bash(1)has an easy feature first introduced incsh(1):will replace the first instance of
oldwithnew:If you want to replace all the instances, that requires more typing:
The
gspecifies the global replacement on the command line. The!refers to a line from history — which could be more specific, if you wanted to pull a command from further back in history that the immediately previous command. Seebash(1)‘s section onEvent Designators.