[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and address@hidden

From: Grisha Levit
Subject: Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and address@hidden
Date: Fri, 17 Mar 2017 17:43:45 -0400

On Tue, Mar 14, 2017 at 9:07 PM, Chet Ramey <address@hidden> wrote:
> when PS1 is expanded the first time, the "current" history entry is the
> one corresponding to the last command entered

> PS2 looks at the current history entry, which is 530 since we've
> started on it.

I think I'm missing something. It seems that when PS1 is expanded \! *does*
match what will eventually become the history number of the command-to-be-
-entered, while PS2 does not. i.e. I can't see how we've started on the second
line of history if the current input will still be stored in the first.

$ PS1='\!      $ ' PS2=${PS1/$/>}; history -c
1      $ : 1.1 \
2      > : 1.2
2      $ fc -l -1
1        : 1.1 : 1.2

> When the first line is entered, the history number and command numbers
> get incremented

There seems to be a mismatch: the history number is incremented and the
command number is not:

$ PS1='\! \#    $ ' PS2=${PS1/$/>} HISTFILE= $BASH --norc -i <<<$':\\\n'
1 1    $ :\
2 1    >

> I'm not sure what the question is.

Fair enough; sorry for the vague report.  I thought it was surprising that:

1. \! and \# increment at different times during command entry
2. The docs refer only to a point in a command's lifecycle at which a prompt
   is displayed and then to a history/command number of "this command":

    PS0    [...] expanded and displayed after reading a command and before the
           command is executed
    PS2    [...] expanded as with PS1 [...]
    PS4    [...] expanded as with PS1 and the value is printed before each
           command bash displays during an execution trace

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

    The history number of a command is its position in the history list [...]
    while the command number is the position in the sequence of commands
    executed during the current shell session.

    Bash expands and displays PS1 before reading the first line of a command
    and expands and displays PS2 before reading [...] subsequent lines [...]
    Bash displays PS0 after it reads a command but before executing it.

This makes clear that the expansions do happen at different times so e.g. \t
should differ since the time changes between the prompts' expansions.
However, a literal reading of the above suggests that `this command' is the
same in all cases and so it's not clear why \! and !# differ.  Perhaps instead
saying of "number of this command" something like "number of the next command
to be read" would be more correct.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]