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.

1 Answer

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:

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like