Linux Bash中的$0脚本文件名参数用法

Bash提供了不同的参数来从不同的源获取输入。 $0 是用于存储 bash脚本的名称 . 这个$0参数由bash或bash解释器自动分配,不能像常规变量一样在脚本文件中更改。这使得它成为bash管理的特殊参数。$0参数只能读取。

null

$0文件名参数示例

由于$0特殊参数是用当前脚本文件名自动分配的,因此不需要初始化脚本。

#!/bin/bash

print "This bash script file name is  $0"

print "The first parameter to this script file is  $1"

当我们执行下面这样的脚本文件时,输出第一行将提供由特殊参数提供的当前脚本文件名 $0 . 第二行提供了该脚本文件提供的第一个参数,该脚本文件存储在 $1 参数。

$ ./myscript.sh 123This bash script file name is ./myscript.shThe first parameter to this script file is 123

我可以设置$0脚本文件名参数吗?

您可能会问$0就像一个变量,我可以设置$0脚本文件名参数吗?无法设置此特殊参数,因为它是只读的,如果尝试为此参数设置新值,将出现如下错误“ /bin/bash:警告:shell级别(1000)过高,正在重置为1 “和” ./myscript.sh:fork:retry:Resource暂时不可用 最后,一段时间后,下列错误将被打印到终端输出 这个bash脚本文件名是./myscript.sh 此脚本文件的第一个参数是=

#!/bin/bash

$0 = "Thisisnewfilename.sh"

echo "This bash script file name is  $0"

echo "The first parameter to this script file is  $1"

让我们执行下面这种容易出错的bash脚本。

$ ./myscript.sh 123

输出如下。

/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1/bin/bash: warning: shell level (1000) too high, resetting to 1./myscript.sh: fork: retry: Resource temporarily unavailable./myscript.sh: fork: retry: Resource temporarily unavailable./myscript.sh: fork: retry: Resource temporarily unavailable./myscript.sh: fork: retry: Resource temporarily unavailable./myscript.sh: fork: Resource temporarily unavailableThis bash script file name is ./myscript.shThe first parameter to this script file is =This bash script file name is ./myscript.shThe first parameter to this script file is =This bash script file name is ./myscript.shThe first parameter to this script file is =This bash script file name is ./myscript.sh

相关文章: chmod Recursively—在Linux中递归地更改文件和文件夹权限

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享