Bash: Update the parent variable from function variable

tak89

i want to update the variable a become 2. But the result is always 1. Is there anyway to do it?

a=1
aple(){
        a=2
        echo "apel"
}
b=`aple`
echo $a
anubhava

Problem is in this call:

b=`aple`

Which invokes aple function in a subshell hence changes made in subshell are lost and not visible in the parent shell.

Call your function as:

aple
echo $a
2

As per your comments if you want to assign a value to b also then have your function as:

a=1
b=
aple() { a=2; b="apel"; }

Then call it as:

aple
echo "$b:$a"
apel:2

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Cannot update a variable inside a function

来自分类Dev

Remove a substring from a bash variable

来自分类Dev

Pass variable from applescript to executable bash file

来自分类Dev

How to update system PATH variable permanently from cmd?

来自分类Dev

undefine a variable in BASH

来自分类Dev

Variable substitution in bash printf {}

来自分类Dev

Seperate values with commas on one line from a variable BASH

来自分类Dev

A delegate for a function with variable parameters

来自分类Dev

bash中的$ {variable //,/}是什么?

来自分类Dev

bash中的$ {variable //,/}是什么?

来自分类Dev

Trying to pass variable to Wordpress function

来自分类Dev

What is the contract of a function or variable in TypeScript

来自分类Dev

Update the Parent Scope from Child Directive

来自分类Dev

Bash: set a shell variable in the middle of the pipeline

来自分类Dev

bash中if [[-n variable]]语法是什么

来自分类Dev

bash-xmlstarlet -v到$ variable

来自分类Dev

Extract value of json-like variable in bash

来自分类Dev

Using bash variable substitution instead of cut/awk

来自分类Dev

Access class variable from instance

来自分类Dev

Mutable variable is accessible from closure

来自分类Dev

Working with a global variable inside a python function

来自分类Dev

Retrieve value by variable name in erlang function

来自分类Dev

C++ static variable in member function

来自分类Dev

“ variable:function(){}”是什么意思?

来自分类Dev

Passing external function of multiple variables as a function of one variable in Fortran

来自分类Dev

bash中[variable =字符串替换]的替代方法

来自分类Dev

Bash如果$ variable; 然后返回0:找不到命令

来自分类Dev

VARIABLE =(){函数定义}如何在bash中工作

来自分类Dev

bash中[variable =字符串替换]的替代方法

Related 相关文章

热门标签

归档