Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and ${var@P}

2017-03-21 Thread Grisha Levit
On Sat, Mar 18, 2017 at 3:21 PM, Chet Ramey  wrote:
> How would you suggest capturing this in the documentation in a way that
> would be clearer?  The text from your previous message or something else?

I'm finding it hard to come up with a concise and accurate description
of the behavior since there's kind of a lot going on..

I took a stab at instead changing the behavior to make \! and \#
consistent for all of a command's prompt strings (patch attached);
perhaps this is a reasonable approach?


consistent-hist-and-cmnd-nums-in-prompts.patch
Description: Binary data


Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and ${var@P}

2017-03-18 Thread Chet Ramey
On 3/17/17 5:43 PM, Grisha Levit wrote:

> Fair enough; sorry for the vague report.  I thought it was surprising that:
> 
> 1. \! and \# increment at different times during command entry

OK, I see what you mean.  They are incremented at different times, and by
different "agents". The command number (\#) is incremented after the shell
parses a complete command and before it is executed.  The history number
(\!) increments when the first line of the command is read and returned and
bash calls add_history.  It's the result of the division of responsibility
between bash and readline.  The shell is the one responsible for adding
subsequent lines to the first history entry (`command-oriented history'),
so the history library has done it's job: it's incremented the history
number and is waiting for the next call to add_history().

Maybe the shell could special-case \! for the second and subsequent lines
of a multi-line command, but that seems tricky to get right.

How would you suggest capturing this in the documentation in a way that
would be clearer?  The text from your previous message or something else?

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and ${var@P}

2017-03-18 Thread Chet Ramey
On 3/17/17 5:43 PM, Grisha Levit wrote:
> On Tue, Mar 14, 2017 at 9:07 PM, Chet Ramey  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.

OK, let me take a look.

> 
> $ 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:

This is true; the command number is incremented only when the complete
command is parsed and before it's executed, before PS0 is displayed.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and ${var@P}

2017-03-17 Thread Grisha Levit
On Tue, Mar 14, 2017 at 9:07 PM, Chet Ramey  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.



Re: \! and \# in PS1 vs PS2 vs PS4, PS0 and ${var@P}

2017-03-14 Thread Chet Ramey
On 3/12/17 5:06 PM, Grisha Levit wrote:
> These expansions both seem to get values that are inconsistent when
> expanded in the various prompt strings.
> 
> For example, starting with the following:
> 
> PS0='PS0 \! \# _ \n'
> PS1='PS1 \! \# $ '
> PS2='PS2 \! \# > '
> PS4='PS4 \! \# + '
> PSV='PSV \! \# V '
> set -x
> 
> The prompts evaluate like so:
> 
> PS1 529 45 $ echo \
> PS2 530 45 > ${PSV@P}
> PS0 530 46 _
> PS4 530 46 + echo PSV 530 46 V
> PSV 530 46 V
> PS1 530 46 $
> 
> i.e. \! is increased as soon as the first line is read, even though
> the subsequent line(s) are to become part of the history entry
> associated with the number displayed in PS1.

I'm not sure what the question is.  It seems reasonable to assume that
when PS1 is expanded the first time, the "current" history entry is the
one corresponding to the last command entered -- nothing has been
entered that would start a new history entry, and the counter doesn't
work ahead like that.

When the first line is entered, the history number and command numbers
get incremented; that's going to be the next (the new "current") history
entry.  PS2 looks at the current history entry, which is 530 since we've
started on it. PS0 and PS4 get 530 because they get expanded after the
complete command is entered and the history entry "finalized", and before
any new history entry is started.

When PS1 is expanded before the next command is read, the situation is
the same as in the first paragraph.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/