# Setting a permanent global variable in unix accessible from any script

**URL:** <https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [September 5, 2011, 6:40am UTC](https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715 "2011-09-05T06:40:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arindamlive](https://community.unix.com/letter_avatar/arindamlive/32/5_5575768a8748004e209b776fc1b2916d.png) [@arindamlive](https://community.unix.com/u/arindamlive)\
**Post date:** [September 5, 2011, 6:40am UTC](https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715/1 "2011-09-05T06:40:07Z")

</div>

Is there anyway in which i can set a permanent global variable in unix, which when initialised with a value and modified during any shell script, would retain its value even if i logout and login

I dont know whether i am being able to express my need clearly but basically what i want is a variable that can be accessed from one script,it may be incremented decremented etc and that the variable would hold its modified value even if i logout and then login and execute the script again

For example if i have a variable x initialised to 10, then if i do x=`expr $x + 1` in one script and then logout and login again, the value of x should be 11

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [September 5, 2011, 6:48am UTC](https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715/2 "2011-09-05T06:48:36Z")

</div>

Short answer: no  
Long answer: not without a lot of hassle and without guarantees. It might be possible by having the shells logout script (if supported) write the value to a file, which is then read by the login script. BUT you'd have to deal with race conditions, and changes done in a non-login non-interactive subshell (eg. when running a script) would not be picked up without adapting that script first to support these extras.

---

<div class="post-metadata">

**Author:** ![arindamlive](https://community.unix.com/letter_avatar/arindamlive/32/5_5575768a8748004e209b776fc1b2916d.png) [@arindamlive](https://community.unix.com/u/arindamlive)\
**Post date:** [September 5, 2011, 6:59am UTC](https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715/3 "2011-09-05T06:59:16Z")

</div>

thanks a lot mate

---

<div class="post-metadata">

**Author:** ![doctorfoo1](https://community.unix.com/letter_avatar/doctorfoo1/32/5_5575768a8748004e209b776fc1b2916d.png) [@doctorfoo1](https://community.unix.com/u/doctorfoo1)\
**Post date:** [September 6, 2011, 7:55pm UTC](https://community.unix.com/t/setting-a-permanent-global-variable-in-unix-accessible-from-any-script/294715/4 "2011-09-06T19:55:10Z")

</div>

yes

your global variable is what ever is current in /tmp/myDumpfile.txt  
just read it. it may change but if it's in a fixed location the information can either be read by others or given to others.  
sample read from file;

#!/bin/sh

echo " READ FROM FILE - LOOP "  
while read f  
do  
echo ""  
echo " Doing something cool"  
/usr/local/sbin/myshizz.sh  
echo " TEST RESULTS for "$f

done \< somedata.txt
