【问题标题】:What does the $# construct mean in bash? [duplicate]bash 中的 $# 结构是什么意思? [复制]
【发布时间】:2011-04-04 21:01:31
【问题描述】:

我明白了

foo() {
if [[ $# -lt 1 ]]; then
    return 0 
fi

...

}

使用 $# 进行比较的具体内容是什么?

【问题讨论】:

    标签: bash scripting shell syntax


    【解决方案1】:

    $# 是传递给脚本的参数数量。有关完整列表,请参阅 bash(1) 手册页的 PARAMETERS 部分的 Special Parameters 小节。

    【讨论】:

      【解决方案2】:

      $# = 传递给函数的参数数量

      在您的代码中,如果未使用至少一个参数调用该函数,则该函数将返回 0。

      【讨论】:

        【解决方案3】:

        $# 表示传递给脚本的命令行参数的数量

        sh-3.2$ cat a.sh
        echo $#  #print the number of cmd line args.
        sh-3.2$ ./a.sh
        0
        sh-3.2$ ./a.sh foo
        1
        sh-3.2$ ./a.sh foo bar
        2
        sh-3.2$ ./a.sh foo bar baz
        3
        

        在函数内部使用时(如您的情况),它表示传递给函数的参数数量:

        sh-3.2$ cat a.sh
        foo() {
                echo $# #print the number of arguments passed to the function.
        }
        foo 1
        foo 1 2
        foo 1 2 3
        
        sh-3.2$ ./a.sh
        1
        2
        3
        

        【讨论】:

          猜你喜欢
          • 2023-03-12
          • 2012-04-19
          • 2021-02-11
          • 2017-03-18
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          • 2013-08-08
          • 1970-01-01
          相关资源
          最近更新 更多