Bash, Set Variable from Script to Memory

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

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

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.