為什麼 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 內建程式一團糟,應該要修復。

相關內容