Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the bash history with exact search, not prefix one?

Tags:

bash

unix

I have two commands I execute frequently. Let's say the first is 'abcd' and the second is 'abc'. So my history contains

1000 abc arg1 arg2 arg3
1001 abcd arg1 arg2 arg3

Now if I type !abcd in bash, it executes the abcd command. which is fine. But if I type !abc in bash, it also executes the last abcd command (since it matches the beginning and is "newer")

How do I use bash history to grab the last abc command? I've tried !'abc ' and "!abc\ " backslashing the space.

like image 630
benhsu Avatar asked Dec 20 '25 03:12

benhsu


1 Answers

You might try reverse history searching: Ctrl-R. On the prompt that appears, type in parts of your commandline (incremental text search).

If you want to switch to the previous match, type some more Ctrl-R.

In your "two similar lines" case,

Ctrl-R abc Ctrl-R

or

Ctrl-R abc<SPACE>

might do it

like image 160
TheBonsai Avatar answered Dec 22 '25 19:12

TheBonsai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!