加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么在bash脚本中需要“declare -f”和“declare -a”?

发布时间:2020-12-16 01:23:20 所属栏目:安全 来源:网络整理
导读:对不起,如此无辜的问题 – 我只是试图承诺… 例如 – 我有: $cat test.sh#!/bin/bashdeclare -f testfuncttestfunct () {echo "I'm function"}testfunctdeclare -a testarrtestarr=([1]=arr1 [2]=arr2 [3]=arr3)echo ${testarr[@]} 当我运行它,我得到: $./
对不起,如此无辜的问题 – 我只是试图承诺…

例如 – 我有:

$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[@]}

当我运行它,我得到:

$./test.sh
I'm function
arr1 arr2 arr3

所以这里是一个问题 – 我必须(如果必须…)在这里插入声明?
有了它 – 或没有它它的工作原理相同…

我可以理解,例如declare -i var或declare -r var.但是什么是-f(声明函数)和-a(声明数组)?

感谢提示和链接.

declare -f 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 :)

所以在你的情况下,声明-f testfunct不会做任何事情,除了可能如果testfunct存在,它将输出它在stdout上的定义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读