Skip to content

builtin/command/exec don't work when invoked as builtins #1541

Closed

Description

@lilyball

The builtin/command/exec builtins are actually handled specially by the parser. They have builtins defined, but those builtins are only responsible for handling e.g. the -h flag. Unfortunately, it's possible to invoke them as builtins in scenarios where they should actually function properly.

The easiest way to do this is to invoke any of them via the builtin builtin. builtin exec echo hi merely sets the exit status to 1. builtin builtin pwd does nothing at all. builtin command ls prints the help for command.

Another way to trigger this is to do what #1398 documents, which is quote the name, e.g. 'command' echo hi (which once again merely prints the help).

A third way is to attempt to use --, e.g. command -- foo. This also prints the help, even though it looks like it should be equivalent to command foo (and is in fact equivalent to that in bash).

A possible solution here might be to teach these three builtins how to behave as expected when invoked as builtins instead of being handled by the parser. The downside to this approach is it contradicts the current behavior of e.g. command $foo being illegal. The upside is that this may be a good change anyway (as covered by issue #564). Furthermore, this would provide a sanctioned way to expand variables into builtins/commands without having to allow $foo bar as as plain statement. If the parser is willing to examine the first string argument to these builtins to find a variable reference, it can use that to fall back to the builtin and allow the parser implementation of these 3 builtins to keep its current simplicity.


This does suggest that other builtins (that are handled by the parser) might need similar treatment. However, I disagree. Those builtins are actually effectively shell keywords (e.g. if, and, etc) and not "regular" builtins. I'm ok with builtin if invoking its current behavior, which is to print its help (although the behavior of these builtins does need some work, e.g. builtin if foo merely returns an exit code of 1 instead of printing its help). Furthermore, the type command could perhaps learn to call these builtins "shell keywords" instead (as Bash does).

Activity

  1. added this to the fish-future milestone on Oct 2, 2014
  2. krader1961 commented on Apr 8, 2016

    @krader1961
    Contributor

    This is fundamentally a duplicate of issue #154.

  3. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions