Bash history substitution

Published by Choan on 2023-02-25

Something I just learnt: you can run a modified command from your bash history.

For a starter, suppose you have just run something like

wp @somesite plugin install --activate some-plugin

(That's wp-cliish for install and activate some-plugin on the site I've aliased as @somesite).

Now, if you wanted to run the same command again, you could just hit UP or type the !! bash history designator, which will expand to the last command entered. But, what if you need to run a slightly modified version of the last command? Enter substitution. With the following command, we'll replace somesite with othersite:

^somesite^othersite^

This alternative will have the same effect

!!:s/@somesite/@othersite
# will give us
# wp @othersite plugin install some-plugin

Learn more about Bash history substitution and how to handle your bash history like a boss.