exported variables bash

Hello all,

I have a question regarding exported variables.

I have two scripts: one main script that calls a second one repeatedly.
On the second one I exported a variable COUNT but on every call to the second script the variable is reinitialized.
The idea is that on every call to the second script the COUNT is incremented but I cannot read the last value.

I used this syntax:

declare -i COUNT
export COUNT

Why this kind of behavior?
How can i solve this problem.

Exports only work in one direction, from parent to child.

So I have to declare the variable in both scripts? Or if I declare only in the first script and I export it is ok?

It means that changes to the variable in the child script cannot affect the variable of the same name in the parent. For example:

parent.sh:
export FOO
FOO=1
echo $FOO
./child.sh
echo $FOO
child.sh:
echo $FOO
FOO=2
echo $FOO

You will get 1, 1, 2, 1. Think of it as the child getting it’s own copy of the variable.

I think I understand.
So If I wanna remember the value of the variable I have to save it to a file or something.I thought that exporting the variable in the first script will remember the variable value between to separate runs.

Generally you should not think of scripts as sharing a common variable space. This is because scripts are separate processes, which are quite different animals to procedures or functions in other programming languages.

The shell language allows you to define functions, you should try those.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

No, you can do that differently. When you exit from the nested (child)
script you could have it return, as its return code or other output, the
value to be set back on the source script. For example:

<code filename=“script0.sh”>
#!/bin/bash
export TESTME=0
SCRIPT0RETURN=0
echo $TESTME
let TESTME=$TESTME+1
echo $TESTME
let TESTME=$TESTME+1
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
</code>

<code filename=“script1.sh”>
#!/bin/bash
echo script1 start $TESTME
let TESTME=$TESTME+1
echo script1 end and return $TESTME
exit $TESTME
</code>

The following output is created when running ./script0.sh:

<quote>
ab@mybox:~/Desktop> ./script0.sh
0
1
script1 start 2
script1 end and return 3
5
script1 start 5
script1 end and return 6
11
script1 start 11
script1 end and return 12
23
2009-07-31 08:13:08 Jobs:0 Err:0
ab@mybox:~/Desktop>
</quote>

Notice the values grow faster over time because of the larger values sent
to, and returned from, script1. That should show you what to do from
here, I think.

Good luck.

ionpetrache wrote:
> ken_yap;2020099 Wrote:
>> It means that changes to the variable in the child script cannot affect
>> the variable of the same name in the parent. For example:
>>
> Code:
> --------------------
> > > parent.sh:
> > export FOO
> > FOO=1
> > echo $FOO
> > ./child.sh
> > echo $FOO
> --------------------
> Code:
> --------------------
> > > child.sh:
> > echo $FOO
> > FOO=2
> > echo $FOO
> --------------------
>> You will get 1, 1, 2, 1. Think of it as the child getting it’s own
>> copy of the variable.
>
> I think I understand.
> So If I wanna remember the value of the variable I have to save it to a
> file or something.I thought that exporting the variable in the first
> script will remember the variable value between to separate runs.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJKcvx/AAoJEF+XTK08PnB5x0MQAJtwW++3JaeqWtRclwWUbEl3
pE0xmdCpZ4+z9KJ1VtP2yDajgoP2m2BWUz9W47Yn3x3qz16MJnxfe0O1DlJaX23D
+vLHqejD/89j+kMb4omlxIo9QWnZ98sO6ySTwluGOAON7Ybu9bqqCyWtGYjo7Wme
Urbf5CpXC9o/JkR3QfEMia+2VX1CKtwPh44ct5tFkqZO8VwmYQ7apKTNxbkHt4ZP
absm5eDDSNzks/Hzo7mge5m9tfKO1N4/8sJP2uHLGJNVtLwhO35+Gj35k5NPCs7V
Az5p9BunHfxqVB6mxOWwHSxyaAtWeQHN12QSvFysMiCgRMslJ5kTsnuWjjAV1fTZ
MDVm8T+f+TlP51qMHrO6B+tFPZBFwu0tklWs2EK1BxA082rUmKFmF133C7ZwPdhb
nhg8n02ijLE76o+giWwlx7wmqYhZq4V5bt3ng1oFyoou41YKl05ukFM31B/fSUft
tyNixJY+j0tKgH+NqnWcV1uW1jb3HvlKWexqQ/hea8hXanHOBstXonFV25ebMZZF
63OV3yeJB1uMc/g+kbpvxZaJNitiX7+xvGKaDtgoNt4+ifOKjcvcryQDMUcKn1NH
5sszsNvRzbekP9sC9kPvXJu5xpDFZ443IQoxGS/jGjUwsZPHlMrFLqA5IP2qwwfy
YWYW/q5MGeZvwz0oKHCz
=XL2I
-----END PGP SIGNATURE-----

Intrigued by this perhaps someone could tell me the why or why nots using named pipes I achieved this?

Parent

$cat parent 
#!/bin/bash
if  -p "namedpipe" ]
    then rm namedpipe
else
   echo "Doesn't Exist"
fi
mkfifo namedpipe
this="LOTS AND LOTS"
echo $this
echo $this > namedpipe &
./child
echo "before pipe( $this )in Parent"
this=$(cat namedpipe)
echo "$this in Parent"

Child

$cat child 
#!/bin/bash
this="The Wrong One"
echo "$this in Child"
this=$(cat namedpipe)
echo "$this in Child"
this="The Modified One"
echo "$this in Child"
echo $this > namedpipe &

Output
on running

$./parent 
LOTS AND LOTS in  Parent
The Wrong One in Child
LOTS AND LOTS in Child
The Modified One in Child
before pipe( LOTS AND LOTS )in Parent
The Modified One in Parent

Thanks for the replies.I’ll try to use the variant with the exit status.It’s easier for me.

Thanks again.