在“$0”和“${BASH_SOURCE [0]}”之间进行选择
来自GNU的这个描述没有帮助我.
- BASH_SOURCE
- An array variable whose members are the source filenames where the
- corresponding shell function names in the FUNCNAME array variable are
- defined. The shell function ${FUNCNAME[$i]} is defined in the file
- ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}
${BASH_SOURCE [0]}(或更简单地说,$BASH_SOURCE)包含所有调用方案中包含脚本的(潜在的相对)路径,特别是在脚本来源时,这对于$0不是真的.
此外,由于Charles Duffy指出,调用者可以将$0设置为任意值.
另一方面,如果没有涉及到命名文件,$BASH_SOURCE可以是空的;例如.:
echo’echo“[$BASH_SOURCE]”’|庆典
以下示例说明了这一点:
脚本foo:
- #!/usr/bin/env bash
- echo "[$0] vs. [${BASH_SOURCE[0]}]"
- $bash ./foo
- [./foo] vs. [./foo]
- $. ./foo
- [bash] vs. [./foo]
$0是POSIX shell规范的一部分,而名称建议的是BASH_SOURCE是特定于Bash的.