简体   繁体   中英

Using events non-interactively in bash

In Bash, I want to get the arguments I used last time I used the command foo . Interactively, I can use an event designator by by typing !foo:* . Is there any way to use events non-interactively, eg, within a function? I managed to get a similar effect this way, but it seems extremely hacky:

$(fc -s foo=echo foo 2> /dev/null)

PS Anyone have any suggestions for more appropriate tags for this question? Are there any tags that have to do with the ! event designator in Bash, or anything similar?

You can use the bash history builtin's -p option:

-p perform history expansion on each ARG and display the result without storing it in the history list

eg: fooargs=$(history -p '!foo:*')

The quote above is from help history

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM