UNIX - 为什么 $# 不包含命令(因此总是 >= 1)?
UNIX - why doesn't $# include the command (thus always being >= 1)?
搜索这个问题的答案有点困难,因为 $#
似乎没有在搜索引擎上正确通过。我很好奇为什么 argv
通常包含命令名称本身,而 $#
不包含。
为了更清楚,如果我有一个名为 testing.sh
的脚本
#!/bin/bash
echo $#
./testing.sh
returns 0
而不是 1
。为什么?
bash
紧随 POSIX specification for $#
:
Expands to the decimal number of positional parameters. The command name (parameter 0) shall not be counted in the number given by '#' because it is a special parameter, not a positional parameter.
shell 的参数接口与 C 的完全不同。在 bash
术语中,您可以定义
argv=( "[=10=]" "$@")
argc=${#argv[@]}
因为 shell(明智地)将命令名称与命令的参数分开。
搜索这个问题的答案有点困难,因为 $#
似乎没有在搜索引擎上正确通过。我很好奇为什么 argv
通常包含命令名称本身,而 $#
不包含。
为了更清楚,如果我有一个名为 testing.sh
的脚本#!/bin/bash
echo $#
./testing.sh
returns 0
而不是 1
。为什么?
bash
紧随 POSIX specification for $#
:
Expands to the decimal number of positional parameters. The command name (parameter 0) shall not be counted in the number given by '#' because it is a special parameter, not a positional parameter.
shell 的参数接口与 C 的完全不同。在 bash
术语中,您可以定义
argv=( "[=10=]" "$@")
argc=${#argv[@]}
因为 shell(明智地)将命令名称与命令的参数分开。