user1508332
user1508332

Reputation:

incrementing an environmental variable

I need to increment an environmental variable by these steps:

envar=1
export envar
sh script_incrementation
echo $envar

where script_incrementation contains something like this:

#! /bin/sh
envar=$[envar+1] #I've tried also other methods of incrementation
export envar

Whatever I do, after exiting the script the variable remains with its initial value 1.

THanks for your time.

Upvotes: 6

Views: 6428

Answers (2)

merlin2011
merlin2011

Reputation: 75545

A shell script executes in its own shell, so you cannot affect the outer shell unless you source it. See this question for details of that discussion.

Consider the following script, which I will call Foo.sh.

#!/bin/bash

export HELLO=$(($HELLO+1))

Suppose in the outer shell, I define an environmental variable:

export HELLO=1

If I run the script like this, it run inside its own shell and will not affect the parent.

./Foo.sh

However, if I source it, it will just execute the commands in the current shell, and will achieve the desired affect.

. Foo.sh
echo $HELLO # prints 2

Upvotes: 15

Sigi
Sigi

Reputation: 1864

Your script can not change the environment of the calling process (shell), it merely inherits it.

So, if you export foo=bar, and then invoke sh (a new process) with your script, the script will see the value of $foo (which is "bar"), and it will be able to change its own copy of it – but that is not going to affect the environment of the parent process (where you exported the variable).

You can simply source your script in the original shell, i.e. run

source increment_script.sh

or

. increment_script.sh

and that will then change the value of the variable.

This is because sourceing a script avoids spawning a new shell (process).

Another trick is to have your script output the changed environment, and then eval that output, for example:

counter=$[counter+1]
echo "counter=$counter"

and then run that as

eval `increment_script.sh`

Upvotes: 0

Related Questions