10

While Googling how to customize my shell prompt via the PS1 variable, I'm seeing tables of special characters that can be used. In particular:

          \!     the history number of this command
          \#     the command number of this command

"History number" seems to be more commonly used, and I know how to use commands like !523 to redo commands from history. But I can't figure out if "command number" has similar functionality. I've tried putting \# in my PS1 variable, and it seems to output the number of commands entered in a particular session (unlike \!, which persists after logout/exit).

Anyone know how to use "command number" in a convenient or meaningful way?

  • 1
    I've searched pretty deeply on the net - near as I can tell, this "command number" is only valuable insofar as it tells you how many commands you've entered. I can't find a way to use this number interactively, like with history expansion – Lagrangian Mar 30 '15 at 21:41
  • Interesting question. If you make that comment an answer, I'd vote for it. – Peter Cordes Apr 19 '15 at 13:36
4

As far as I can tell (and this seems confirmed by your research), there is no way to refer to that magic number interactively, or not through fc or !n shortcuts. Those certainly seem to refer only to the absolute position in the history list, not the relative position since this specific shell started (which is waht \# refers to, as you correctly pointed out).

The only way i found to make this nicer here is to set the following:

export HISTFILESIZE=1001
export HISTSIZE=-1

That way:

  1. a new session's history starts at 1000, which makes it easier to identify where i'm at in a session
  2. (somewhat unrelated) i don't lose older history in a given session (but still don't flood the file)

Basically, it turned my modified prompt (PS1="\\!$ ") from:

499$ 

to:

1000$ 

... which makes it a little cleaner on start. But that's probably not the answer you were looking for. :)

(By the way, I also looked at zsh for a solution, and it seems it simply doesn't have the equivalent of \#, so that doesn't help at all either.)

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.