13

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?

2
  • 2
    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, 2015 at 21:41
  • 1
    Interesting question. If you make that comment an answer, I'd vote for it. Apr 19, 2015 at 13:36

2 Answers 2

6

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.)

2

Bash's command number is for display only.

First, some background from bashref:

The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (*note Bash History Facilities::), while the command number is the position in the sequence of commands executed during the current shell session.

Diving through the source, in parse.y we see that '\#' resolves to the global static variable current_command_number:

case '#':                                                                     
  n = current_command_number;                                                 
  /* If we have already incremented current_command_number (PS4,              
 ${var@P}), compensate */                                                     
  if (orig_string != ps0_prompt && orig_string != ps1_prompt && orig_string != ps2_prompt)
n--;                                                                          
  temp = itos (n);                                                            
  goto add_string;                                                            

which only has one other use: in eval.c, it's incremented upon running a command:

# ...
current_command_number++;                                                  

executing = 1;                                                             
stdin_redir = 0;                                                           

execute_command (current_command);                                         

All that's kept is a number, not the actual command or even the equivalent history number. So, upon execution of each command, bash forgets what command associated with what command number, rendering the command number unusable for anything other than display and scroll reference.

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.