为什么 bash 脚本中需要“declare -f”和“declare -a”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19742062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are "declare -f" and "declare -a" needed in bash scripts?
提问by setevoy
Sorry for so innocent question - I just try to undertand...
对不起,这么无辜的问题 - 我只是想了解......
For example - I have:
例如 - 我有:
$ cat test.sh
#!/bin/bash
declare -f testfunct
testfunct () {
echo "I'm function"
}
testfunct
declare -a testarr
testarr=([1]=arr1 [2]=arr2 [3]=arr3)
echo ${testarr[@]}
And when I run it I get:
当我运行它时,我得到:
$ ./test.sh
I'm function
arr1 arr2 arr3
So here is a question - for what i must (if must...) insert declare
here?
With it - or without it it works the same...
所以这里有一个问题 - 我必须(如果必须......)插入declare
这里?有了它 - 或者没有它,它的工作原理是一样的......
I can understand for example declare -i var
or declare -r var
. But for what is -f
(declare function) and -a
(declare array)?
我可以理解例如declare -i var
或declare -r var
。但是对于什么是-f
(declare function) 和-a
(declare array)?
Thanks for tips and links.
感谢您的提示和链接。
采纳答案by gniourf_gniourf
declare -f functionname
is used to output the definition of the function functionname
, if it exists, and absolutely not to declarethat functionname
is/will be a function. Look:
declare -f functionname
用于输出的函数的定义functionname
,如果它存在,并且绝对不宣布即functionname
是/将是一个函数。看:
$ unset -f a # unsetting the function a, if it existed
$ declare -f a
$ # nothing output and look at the exit code:
$ echo $?
1
$ # that was an "error" because the function didn't exist
$ a() { echo 'Hello, world!'; }
$ declare -f a
a ()
{
echo 'Hello, world!'
}
$ # ok? and look at the exit code:
$ echo $?
0
$ # cool :)
So in your case, declare -f testfunct
will do nothing, except possibly if testfunct
exists, it will output its definition on stdout.
因此,在您的情况下,declare -f testfunct
将不执行任何操作,除非可能testfunct
存在,否则它将在 stdout 上输出其定义。
回答by eminor
As far as I know, the -a option alone does not have any practical relevance, but I think it's a plus for readability when declaring arrays. It becomes more interesting when it is combined with other options to generate arrays of a special type.
据我所知,单独的 -a 选项没有任何实际意义,但我认为它在声明数组时增加了可读性。当它与其他选项结合以生成特殊类型的数组时,它会变得更有趣。
For example:
例如:
# Declare an array of integers
declare -ai int_array
int_array=(1 2 3)
# Setting a string as array value fails
int_array[0]="I am a string"
# Convert array values to lower case (or upper case with -u)
declare -al lowercase_array
lowercase_array[0]="I AM A STRING"
lowercase_array[1]="ANOTHER STRING"
echo "${lowercase_array[0]}"
echo "${lowercase_array[1]}"
# Make a read only array
declare -ar readonly_array=(42 "A String")
# Setting a new value fails
readonly_array[0]=23
回答by Idriss Neumann
declare -f
allows you to list all defined functions (or sourced) and their contents.
declare -f
允许您列出所有定义的函数(或来源)及其内容。
Example of use:
使用示例:
[ ~]$ cat test.sh
#!/bin/bash
f(){
echo "Hello world"
}
# print 0 if is defined (success)
# print 1 if isn't defined (failure)
isDefined(){
declare -f "" >/dev/null && echo 0 || echo 1
}
isDefined f
isDefined g
[ ~]$ ./test.sh
0
1
[ ~]$ declare -f
existFunction ()
{
declare -f "" > /dev/null && echo 0 || echo 1
}
f ()
{
echo "Hello world"
}
However as smartly said gniourf_gniourf below : it's better to use declare -F
to test the existence of a function.
然而,正如下面的 gniourf_gniourf 聪明地说:最好declare -F
用来测试一个函数的存在。