[Top][All Lists]

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

Re: How get last commnda history line number directly

From: Chet Ramey
Subject: Re: How get last commnda history line number directly
Date: Tue, 8 Sep 2020 09:50:18 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.12.0

On 9/6/20 4:18 PM, L A Walsh wrote:

> history 3
> 2983  echo "this is on what history line? ($HISTCMD)"
> 2984  echo $(($HISTCMD-2))
> 2985  history 3
> Seems to only give correct line from history by subtracting 2?
> Maybe its different in 5.x?

When readline stores history entries, it stores them in an array starting
from 0, and it increments the history number after storing an entry so it's
an accurate count of the number of entries in the list. That means the
history number has to be offset somehow if it's already been stored and the
number of entries has been incremented.

When bash implements command-oriented history, it has to manage using the
same history entry as the first line of the command, after the history
library has already incremented the history number. The same thing happens
when it implements literal history.

There are two cases to consider: the expansion is being performed as part
of PS1 or another prompt expansion, in which case the shell should use the
current history number (unless the line is part of a multi-line command),
or whether it's being expanded as part of command execution, in which case
the history entry has already been stored and the history number has to be

I made several changes in early 2017 based on


and bash-5.1 will have additional changes for $HISTCMD.

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

reply via email to

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