Variable in Bash Script that keeps it value from the last time running
I'm afraid you have to save the state in a file somewhere. The trick is to put it somewhere the user will be able to write to.
yourscriptvar=0
if [ -e "$HOME/.yourscriptvar" ] ; then
yourscriptvar=$(cat "$HOME/.yourscriptvar")
fi
# do something in your script
#save it output the file
echo $yourscriptvar > "$HOME/.yourscriptvar"
You can't, but you can use a file to do it
#!/bin/bash
valuefile="/tmp/value.dat"
# if we don't have a file, start at zero
if [ ! -f "$valuefile" ]; then
value=0
# otherwise read the value from the file
else
value=$(cat "$valuefile")
fi
# increment the value
value=$((value + 1))
# show it to the user
echo "value: ${value}"
# and save it for next time
echo "${value}" > "$valuefile"