1

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 1

2

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
  • Probably not applicable for a script run as normal user. You could use /tmp or /var/tmp, though. You won't notice the disk overhead (/tmp is not a memory based file system).
    – vanadium
    Jun 1, 2021 at 13:17
  • Thanks, yes, I tested these examples, but it is not what exactly I need. At the end, I did via storing to file, at the end, linux is based on files. ;)
    – genderbee
    Jun 1, 2021 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .