Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: export not passing variables correctly to parent

The variables exported in a child-script are undefined the parent-script (a.sh):

#!/bin/bash
# This is the parent script: a.sh
export var="5e-9"
./b.sh var
export result=$res;  # $res is defined and recognized in b.sh
echo "result = ${result}"

The child-script (b.sh) looks like this:

#!/bin/bash
# This is the child script: b.sh
# This script has to convert exponential notation to SI-notation
var1=$1
value=${!1}
exp=${value#*e}
reduced_val=${value%[eE]*}
if [ $exp -ge -3 ] || [ $exp -lt 0 ]; then SI="m";
elif [ $exp -ge -6 ] || [ $exp -lt -3 ]; then SI="u";
elif[ $exp -ge -9 ] || [ $exp -lt -6 ]; then SI="n";
fi

export res=${reduced_val}${SI}
echo res = $res

If I now run the parent using ./a.sh, the output will be:

res = 5n
result = 4n

So there is some rounding problem going on here. Anybody any idea why and how to fix it?

like image 758
Bjorn Avatar asked Feb 13 '23 01:02

Bjorn


1 Answers

To access variable's in b.sh use source instead :

source b.sh var

It should give what you wanted.

like image 116
blackSmith Avatar answered Feb 16 '23 01:02

blackSmith