1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

if i still want to use b=apel, is there anyway to update it?
b=`apel` will always create a sub shell hence changes will be lost in parent shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.