bash 检查参数是否定义
I would like to know whether a function, say f1()
, has already been defined in Bash. How should I do it?
我想知道是否已经在Bash中定义了一个函数f1()
。 我该怎么办?
You can use the type -t
to get a string of the type of the name:
您可以使用type -t
来获取名称类型的字符串 :
If the -t option is used, type prints a string which is one of alias, keyword, function, builtin, or file if name is an alias, shell reserved word, function, builtin, or disk file, respectively.
如果使用-t选项,则类型将打印一个字符串,该字符串是别名,关键字,函数,内置文件或文件之一,如果名称是别名, shell保留字,函数,内置文件或磁盘文件。
So this piece of code check whether a function has already been defined:
因此,这段代码检查是否已定义函数:
tf=$(type -t f1 2>/dev/null || rt=$?)
if [[ "$tf" != "function" ]]; then
echo "f1 is defined as a function"
fi
The rt=$?
here makes sure that statement’s return code is 0 so that it works well with set -o errexit
.
rt=$?
这里确保该语句的返回码为0,以便它与set -o errexit
一起很好地工作。
翻译自: https://www.systutorials.com/how-to-check-whether-a-function-has-been-defined-in-bash/
bash 检查参数是否定义