3

I'm looking for a character to put in my zsh's $PROMPT that would be equivalent to bash's \# (the command number of this command, according to this site). So far I've found only %h but this is not what I'm looking for, it's equivalent to \!.

shrx
  • 474

1 Answers1

1

As you already screened the corresponding man page and didn't find anything suitable, I'll present a alternative method:

  1. setopt PROMPT_SUBST, so parameter expansion, command substitution and arithmetic expansion are performed in prompts.

  2. Increment a varable cmdcount (initialized with 1) for each executed command via the preexec hook: Executed just after a command has been read and is about to be executed. (...)

  3. Use $cmdcount in your prompt.


Copy & Paste code for your ~/.zshrc:

setopt PROMPT_SUBST
[[ $cmdcount -ge 1 ]] || cmdcount=1
preexec() { ((cmdcount++)) }
PS1='$cmdcount '                # notice the single(!) tics

And here is how it works (left = bash with \# in PROMPT, right = zsh with proposed code):

bash$ PS1="\# "                                   |  zsh$ source ./above_code
2 /bin/echo some external command                 |  1 /bin/echo some external command
some external command                             |  some external command
3 cd internal command                             |  2 cd internal command
bash: cd: internal: No such file or directory     |  cd: string not in pwd: internal
4      [no command, just pressed enter]           |  3      [no command, just pressed enter]
4                                                 |  3 
4                                                 |  3

I do not know about the specific behavior of \# in bash, but at first glance both shells behave similar now.

mpy
  • 28,816