简体   繁体   中英

history -r has different behaviour if called by bind or if typed in command line

history -r , used in a function, has different behaviour depending on whether it's called by command line or with bind.

With a little file called hist.txt containing 4 lines:

cat hist.txt
a
b
c
d

and a function called foo :

function foo {
  history -c && history -r ./hist.txt
}

If I type foo and then press UP key, I get the buffer filled with d . This is correct.

If I bind this function with bind -x '"\\eW":"foo"' then press ALT+W , foo is executed but the result is different: if I press UP key I get nothing, and if I press DOWN key then I get b .

Is there an explanation for this, and is there perhaps a way for this bind to act like the first behavior described?

Auto-answer. I still don't know why history -r does not comply with bind but I found a workaround using a loop and history -s :

bind -x '"\eW":"dum=$(cat hist.txt) && while read j; do history -s ${j}; done <<< $dum"'

Only difference with history -r is that hist.txt file needs to end with an empty line. If not : read will not get the last line ( d ) in the provided example.

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