在$ 0和BASH_SOURCE之间选择 [英] choosing between $0 and BASH_SOURCE

查看:91
本文介绍了在$ 0和BASH_SOURCE之间选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在"$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]}

解决方案

注意:有关POSIX兼容解决方案,请参见这个答案.

${BASH_SOURCE[0]}(或更简单地说,是$BASH_SOURCE [1] )包含在 all 调用场景中包含脚本的(可能是相对的)路径,尤其是在脚本是 sourced 的情况下(对于$0而言不是). >

此外,正如 Charles Duffy 指出的那样,$0可以设置为任意值.
另一方面,如果不涉及命名文件,则$BASH_SOURCE 可以为空.例如:
echo 'echo "[$BASH_SOURCE]"' | bash

以下示例对此进行了说明:

脚本foo:

#!/bin/bash
echo "[$0] vs. [${BASH_SOURCE[0]}]"


$ bash ./foo
[./foo] vs. [./foo]

$ ./foo
[./foo] vs. [./foo]

$ . ./foo
[bash] vs. [./foo]

$0是POSIX Shell规范的一部分,而顾名思义,BASH_SOURCE是Bash特定的.


[1] 可选阅读:${BASH_SOURCE[0]}$BASH_SOURCE :

Bash允许您使用 scalar 表示法引用 array 变量的元素0.换句话说:如果您引用变量就像它是一个标量,则该元素的索引为0.

使用此功能掩盖了$arr是一个数组的事实,这就是为什么流行的shell代码linter shellcheck.net 发出以下警告(在撰写本文时):

SC2128:扩展没有索引的数组只会得到第一个元素.

附带说明:虽然此警告很有帮助,但它可能更精确,因为您不一定会获得 first 元素:特别是索引0处的元素返回,因此,如果第一个元素具有更高的索引(在Bash中可能是更高的索引),则会得到空字符串;尝试'a[1]='hi'; echo "$a"'.
(通过对比,zsh,无论叛军,确实确实都会返回第一个元素,而不管其索引如何.)

由于它的晦涩难懂,您可以选择避开此功能,但是它可以预见地工作,并且从实用的角度来说,您几乎不需要访问数组0之外的其他 索引变量${BASH_SOURCE[@]}.

How does one choose between "$0" and "${BASH_SOURCE[0]}"

This description from GNU didn't help me much.

    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]}

解决方案

Note: For a POSIX-compliant solution, see this answer.

${BASH_SOURCE[0]} (or, more simply, $BASH_SOURCE[1] ) contains the (potentially relative) path of the containing script in all invocation scenarios, notably also when the script is sourced, which is not true for $0.

Furthermore, as Charles Duffy points out, $0 can be set to an arbitrary value by the caller.
On the flip side, $BASH_SOURCE can be empty, if no named file is involved; e.g.:
echo 'echo "[$BASH_SOURCE]"' | bash

The following example illustrates this:

Script foo:

#!/bin/bash
echo "[$0] vs. [${BASH_SOURCE[0]}]"


$ bash ./foo
[./foo] vs. [./foo]

$ ./foo
[./foo] vs. [./foo]

$ . ./foo
[bash] vs. [./foo]

$0 is part of the POSIX shell specification, whereas BASH_SOURCE, as the name suggests, is Bash-specific.


[1] Optional reading: ${BASH_SOURCE[0]} vs. $BASH_SOURCE:

Bash allows you to reference element 0 of an array variable using scalar notation: instead of writing ${arr[0]}, you can write $arr; in other words: if you reference the variable as if it were a scalar, you get the element at index 0.

Using this feature obscures the fact that $arr is an array, which is why popular shell-code linter shellcheck.net issues the following warning (as of this writing):

SC2128: Expanding an array without an index only gives the first element.

On a side note: While this warning is helpful, it could be more precise, because you won't necessarily get the first element: It is specifically the element at index 0 that is returned, so if the first element has a higher index - which is possible in Bash - you'll get the empty string; try 'a[1]='hi'; echo "$a"'.
(By contrast, zsh, ever the renegade, indeed does return the first element, irrespective of its index).

You may choose to eschew this feature due to its obscurity, but it works predictably and, pragmatically speaking, you'll rarely, if ever, need to access indices other than 0 of array variable ${BASH_SOURCE[@]}.

这篇关于在$ 0和BASH_SOURCE之间选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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