In bash, history -p does history expansion on its argument; What is the alternative in zsh?

2 Answers 2


I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.

$ setopt histverify
$ echo !!Enter
$ echo setopt histverifyEnter
setopt histverify

Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).

And the whole history is accessible with the $history special associative array.

Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.

So ${history:0:1} is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be ${${(z)history:0:1}[-1]:h}


In bash you can expand a command by doing this:

history -p !23 

So if in 23 you have ls -l the, return will be:


In zsh i am able to reproduce a similar (but not identical) behavior by doing this:

history 23 23 | xargs -n1 | tail -n+2
  • 1
    ITYM history -p '!23'. In zsh, just use print -r -- $history[23] Sep 3, 2018 at 15:02
  • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =( Sep 3, 2018 at 15:04
  • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E${(l:2::0:)$((match[1]+1))}'. I wrote that function using fc in the end.
    – HappyFace
    Sep 4, 2018 at 17:49
  • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution! Sep 4, 2018 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.