为什么 Bash 的内置参数是可选的?

为什么 Bash 的内置参数是可选的?

运行只是builtin不打印任何内容并返回退出代码 0。这与 一致help builtin,它将所有参数显示为可选。但为什么这个空操作不是一个错误呢?有这方面的用例吗?更有用的结果是错误代码,或者更好的是列出当前可用的内置函数。

答案1

Bash 内置函数不一致且文档缺乏。

这是一个例子:

$ help command
command: command [-pVv] command [arg ...]
    Runs COMMAND with ARGS ignoring shell functions.  If you have a shell
    function called 'ls', and you wish to call the command `ls', you can
    say "command ls".  If the -p option is given, a default value is used
    for PATH that is guaranteed to find all of the standard utilities.  If
    the -V or -v option is given, a string is printed describing COMMAND.
    The -V option produces a more verbose description.
$ command; echo $?
0

即使没有command返回码$? -eq 0,也没有错误std err

另一个:

$ help disown
disown: disown [-h] [-ar] [jobspec ...]
    By default, removes each JOBSPEC argument from the table of active jobs.
    If the -h option is given, the job is not removed from the table, but is
    marked so that SIGHUP is not sent to the job if the shell receives a
    SIGHUP.  The -a option, when JOBSPEC is not supplied, means to remove all
    jobs from the job table; the -r option means to remove only running jobs.
$ disown; echo $?
-bash: disown: current: no such job
1

所有参数都是可选的,但当$? -eq 1没有参数时它会返回。

我什至编译了最新的 Bash 4.2,以下是我的结果:

$ help command
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.

    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.

    Options:
      -p    use a default value for PATH that is guaranteed to find all of
        the standard utilities
      -v    print a description of COMMAND similar to the `type' builtin
      -V    print a more verbose description of each COMMAND

    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.
$ command; echo $?
0

有一个新部分“退出状态”,并且command仍然是一个可选参数。甚至比 3.x 还差。其他内置程序也是如此。

所以,你是对的。 Bash 内置程序一团糟,应该修复。

相关内容