Environment variable need to be incremented

Dear All,

I have created a environment variable say VAR and initialised it to 0.when i do
echo $VAR it is showing 0.I have written a shell script with one line VAR=$((VAR+1))
and after running the shell script when i echo $VAR my output is still 0.I want to be 1.
when i run the statement VAR=$((VAR+1)) without writing in shell script and when i echo it the value is changed to 1.
why is it like when i run the statement in a shell script the value is not getting effected.

Because your shell script's env is not the SAME as your terminal environment. The shell with which the script is invoked will clone its default environment for the use of script and the changes that you do inside the script is for the script's env only and doesnt impact the terminal's env from which its invoked.

step 1: Run the process, which makes a complete independent copy of all the environment variables in your shell.

step 2: Increment the independent copy of that shell variable.

step 3: The shell dies, taking that copy with it.

If you want to change the variable in your own shell, you'll have to change the variable in your own shell, by sourcing a script or running an alias or function or the like.

where the script file increments the counter...

export FOO=0; . ./<script-file>