在Bash中使用声明内部函数 [英] Using Declare Inside a Function in Bash

查看:130
本文介绍了在Bash中使用声明内部函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个函数改变一个全局变量(或者至少追加它)。

  input =Hello
示例=输入

func(){
declare -x $ example =$ {input} World
}

func
echo $ input

这个输出将是Hello的原始值。如果函数能够更改原始值,我希望它。是否有替代完成这一点。请注意我需要设置 example = input ,然后对example(变量)执行操作。

顺便说一句,如果我使用 eval ,那么函数会抱怨World是一个函数或其他东西。

解决方案

您是否尝试使用导出?

  export $ example =$ {input} World


I am want to change a global variable (or at least append to it) using a function.

input="Hello"
example=input    

func() {
    declare -x $example="${input} World"
}

func
echo $input

The output of this would be "Hello" The original value. I would like it if the function were to able to change the original value. Is there alternative to accomplishing this. Please note I need to set example=input and then perform on the operation on example (the variable).

BTW, if I used eval instead the function will complain about World being a function or something.

解决方案

Did you try using export?

export $example="${input} World"

这篇关于在Bash中使用声明内部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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