"$#"代表什么卑鄙的意思? [英] What does "$#" mean in bash?

查看:452
本文介绍了"$#"代表什么卑鄙的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此相关的脚本:

I have a script with this:

login {
    # checking parameters -> if not ok print error and exit script
    if [ $# -lt 2 ] || [ $1 == '' ] || [ $2 == '' ]; then
        echo "Please check the needed options (username and password)"
        echo ""
        echo "For further Information see Section 13"
        echo ""
        echo "Press any key to exit"
        read
        exit
    fi

  } # /login

但是我真的不知道$#在第三行的含义.

But I really dont know what the $# means on the third line.

推荐答案

井号很重要.

  1. 如果只是$#,则是位置参数的数量,例如$1$2$3. (请注意,不算$0.)
  2. 如果为${#var},则为参数扩展中的字符数. (字符串长度)
  3. 如果为${#var[@]},则为数组中元素的数量.由于bash数组稀疏,因此可以与最后一个元素的索引加一个不同.
  1. If it's just $#, it's the number of positional parameters, like $1, $2, $3. (Not counting $0, mind you.)
  2. If it's ${#var}, it's the number of characters in the expansion of the parameter. (String length)
  3. If it's ${#var[@]}, it's the number of elements in the array. Since bash arrays are sparse, this can be different from the index of the last element plus one.

这篇关于"$#"代表什么卑鄙的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆