bash, set variable from script to memory
I need "set" variable from script to memory (global) and read it again in second run of this script with defined last state of this variable.
Script.
echo $count
let "count=count+1"
export $count
echo $count
This shows me 1
when I run this script again. I need 1
, and in the case I run the script again, I need 2
, in case of third run 3
, etc.
I thought export
do this, but it is not working.
Thanks.
The export
shell builtin only exports variables (and their values) to child processes of the current shell.
AFAIK you'd need to use some kind of state file to store a value between invocations. You can locate the file on a RAM-based filesystem such as /var/run
if you want the value to be stored "to memory".
See for example:
-
Is it possible to have bash variables persist between runs
-
Variable in Bash Script that keeps it value from the last time running