I'm working with a command-line tool that provides a number of subcommands that all use the same binary, e.g. tool foo
, tool bar
, etc, and as I work, these commands are placed into my Bash shell history, for example:
7322 [2021-04-16 15:37:45 +0000] tool foo .
7323 [2021-04-16 15:37:47 +0000] tool bar
7324 [2021-04-16 15:37:50 +0000] tool baz
7325 [2021-04-16 15:38:01 +0000] tool qux -a -b asdf -c=100 /var/lib/foo/...
7326 [2021-04-16 15:38:15 +0000] htop
7327 [2021-04-16 15:38:21 +0000] tool foo . -x
The exact tool is proprietary (its exact functionality is irrelevant) and doesn't have specific features to help with tracking and recalling its own commands.
For example, I'd like to recall command 7325, tool qux -a -b asdf -c=100 /var/lib/foo/...
(suppose that it's not recent enough to just hit the up arrow a bunch of times). The commands and parameters vary often enough that establishing a Bash alias doesn't seem practical or convenient (either I edit .bashrc or I lose the alias when the shell closes). I'm pretty confident that the last time I ran tool qux
, it had the correct parameters that I would want to use, or a reasonably safe set that I would need to edit anyway.
I know I could run history | grep qux
to look for the history index and then run !7325
. Is there a way I can directly recall it with one set of keystrokes typed into the Bash prompt? !tool qux
doesn't work because in this scenario it will run tool foo . -x qux
instead. I tried quoting it, but it looks like expansion happens earlier.