I want to be able to read and write to a file with a bash script. I am able to read the variables from the file but I am not sure how to write to it.
The file data.txt
contains the variable names and their values like this:
var1=2
var2=3
The shell script looks like this:
#!bin/bash
echo "Read"
var1=$(grep -Po "(?<=^var1=).*" data.txt)
echo "Do Something"
var2=${var1}
echo "Write"
# var2 should be saved in data.txt like this: var2=<Here is the Value>, e.g. var2=2
I can read the specific values of the variables with the grep
command, which I got from this answer. But I am not sure how to implement the functionality to be able to save the values of the variables at the correct position (see the last comment in the bash script).
The variable names are unique in data.txt
.
Any ideas how to achieve this?