[PATCH] fc: trim range instead of erroring out

23 views
Skip to first unread message

Martijn Dekker

unread,
Aug 11, 2020, 12:00:08 PM8/11/20
to Bug reports for the GNU Bourne Again SHell
As I use/try/test many shells, I'm in the habit of using POSIX commands
such as 'fc -l 1' to list the complete history.

If there have been more than $HISTSIZE command, the list is trimmed at
the beginning without renumbering, and bash errors out:

$ fc -l 1
bash-5.0: fc: history specification out of range

This is different from every other shell, and also looks like it's
contrary to the POSIX spec:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html#tag_20_44_05
| When a range of commands is used, it shall not be an error to specify
| first or last values that are not in the history list; fc shall
| substitute the value representing the oldest or newest command in the
| list, as appropriate. For example, if there are only ten commands in
| the history list, numbered 1 to 10:
|
| fc -l
| fc 1 99
|
| shall list and edit, respectively, all ten commands.

The attached patch removes the error and limits the range to available
commands as specified.

- Martijn

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh
fc.patch

Chet Ramey

unread,
Aug 11, 2020, 2:27:37 PM8/11/20
to Martijn Dekker, Bug reports for the GNU Bourne Again SHell, chet....@case.edu
On 8/11/20 11:59 AM, Martijn Dekker wrote:
> As I use/try/test many shells, I'm in the habit of using POSIX commands
> such as 'fc -l 1' to list the complete history.
>
> If there have been more than $HISTSIZE command, the list is trimmed at the
> beginning without renumbering, and bash errors out:
>
> $ fc -l 1
> bash-5.0: fc: history specification out of range
>
> This is different from every other shell, and also looks like it's contrary
> to the POSIX spec:
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html#tag_20_44_05

Thanks for the report and fix.

Chet

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

Chet Ramey

unread,
Aug 12, 2020, 11:40:16 AM8/12/20
to Martijn Dekker, Bug reports for the GNU Bourne Again SHell, chet....@case.edu
On 8/11/20 11:59 AM, Martijn Dekker wrote:

> This is different from every other shell, and also looks like it's contrary
> to the POSIX spec:
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html#tag_20_44_05
>
> | When a range of commands is used, it shall not be an error to specify
> | first or last values that are not in the history list; fc shall
> | substitute the value representing the oldest or newest command in the
> | list, as appropriate. For example, if there are only ten commands in
> | the history list, numbered 1 to 10:
> |
> | fc -l
> | fc 1 99
> |
> | shall list and edit, respectively, all ten commands.

What's your opinion about what the `as appropriate' means? An out-of-range
`first' gets substituted with the first command in the history, and an out-
of-range `last' gets the last history entry? Bash does one thing, your
patch does another, but neither one does that.

Martijn Dekker

unread,
Aug 20, 2020, 9:08:47 AM8/20/20
to bug-...@gnu.org, Chet Ramey
Op 12-08-20 om 16:40 schreef Chet Ramey:
> What's your opinion about what the `as appropriate' means? An out-of-range
> `first' gets substituted with the first command in the history, and an out-
> of-range `last' gets the last history entry?

Yes, that was my intention.

> Bash does one thing, your
> patch does another, but neither one does that.

Then I suppose my patch was wrong. It seemed to work for me.

Chet Ramey

unread,
Aug 20, 2020, 11:43:41 AM8/20/20
to Martijn Dekker, bug-...@gnu.org, chet....@case.edu
On 8/20/20 9:08 AM, Martijn Dekker wrote:
> Op 12-08-20 om 16:40 schreef Chet Ramey:
>> What's your opinion about what the `as appropriate' means? An out-of-range
>> `first' gets substituted with the first command in the history, and an out-
>> of-range `last' gets the last history entry?
>
> Yes, that was my intention.

OK. That was my question, since that doesn't seem to happen. With your
patch (and even before), for instance, consider the following commands
on an empty history:

echo a
echo b
fc -l
fc -l 1002 1100

The first `fc' will list the echo commands. The second `fc' will list the
last command in the history, since both numbers are clamped at the end of
the history -- the range ends up being `last-last'.

Chet
Reply all
Reply to author
Forward
0 new messages