bash—Better way to store variable between runs?

Posted by shardbearer on Stack Overflow See other posts from Stack Overflow or by shardbearer
Published on 2012-09-08T21:07:08Z Indexed on 2013/11/01 9:55 UTC
Read the original article Hit count: 139

Filed under:
|
|
|

I've made a bash script which I run every hour with crontab, and I need to store one variable so that I can access it the next time I run it. The script changes the variable every time it runs, so I can't hardcode it in. Right now I am writing it to a txt file and then reading it back. Is there a better way to do it than this? And the way I am reading the txt file is something I found on here, I don't understand it, and it's kinda clunky. Is there not a built in command for this? Anyway, here's the applicable code, with some of the variables changed to make it easier to read.

while read x; do
  var=$x
done < var.txt

# Do some stuff, change var to a new value

echo $var > var.txt

The variable is only a single integer, so the text file feels overkill.

© Stack Overflow or respective owner

Related posts about bash

Related posts about variables