shell - zsh function wrapping grep output only returns a few lines -
i'm trying create small zsh function allows me grep history - hgrep test
should return every command i've typed containts test
.
on machine returns 6 results. when type history | grep test
lot more results. gives?
hgrep (){ history | grep $1 }
the output - complete!
➜ ~ hgrep test 7887 mkd test 7889 rm -r test 7894 hgrep test 7896 history | grep test
this incomplete output. also, notice first results much earlier
➜ ~ history | grep test 252 cp mgroup /test 254 cp mgroup /test 322 vi test.js 324 node test.js 325 vi test.js ...
after poking around appears difference in behavior related when .zshrc sourced. on new terminal see unwanted behavior. if source ~/.zshrc
works.
however, i'm still confused why happen.
it seems history
command alias
ed else (for example fc -l 1
) after definition of hgrep
function.
so, put hgrep
function definition after alias
setups. or, define hgrep
function (and functions) not affected aliases, shell options etc.
tl;dr
the command history
lists last 16 events default.
history
same
fc -l
.
[…]
fc -l ... [ first [ last ] ]
if first not specified, set -1 (the recent event), or -16 if -l flag given.
and aliases checked , expanded @ read time.
every eligible word in shell input checked see if there alias defined it. if so, replaced text of alias if in command position
[…]
aliases expanded when code read in; entire line read in 1 go
it seems thehgrep
function uses shell-builtin command history
shows few events , results may differ using history
(which alias
else) interactively.
the hgrep
function redefined source ~/.zshrc
, history
in function expanded/replaced alias
@ point. check resulting function issuing below command:
% functions hgrep hgrep () { … (hgrep function's definition shown)
Comments
Post a Comment