Those commands do not modify any files - they read a file if it exists, then exports a variable in the current context only, and prints the value to standard output (usually the terminal).
Modifying a dotfile programmatically is not recommended, because there are a host of subtle bugs you can encounter both because of the mix of actual code with printed code (in the modifying script) and because you essentially can't be sure that the target file contains something which makes your script behave unexpectedly. This, coupled with the danger of not being able to log in as your normal user, are good reasons to avoid this in general. You have been warned.
To actually do this, you could simply add a new line with the new value of the variable at the end of the file:
echo "USER_PROMPT=haloooo" >> "$VARIABLE_FILE"
This will result in duplicate entries, but if you are just putting text and variables in the variable definition it should at least be fast, and pretty safe as long as your file always ends with a newline.
A more advanced version would be to replace the line if it exists:
if grep '^USER_PROMPT=' -- "$VARIABLE_FILE"
then
sed -i -e 's/^USER_PROMPT=.*/USER_PROMPT=haloooo/' -- "$VARIABLE_FILE"
else
echo "USER_PROMPT=haloooo" >> "$VARIABLE_FILE"
fi
Be warned that if you want to put arbitrary text in the replacement value, you have to make sure to escape it for sed
. Also, this command will overwrite every line which starts with USER_PROMPT=
, so if you want it to have different values at different times during your script (variable reuse is generally evil) you just introduced a bug. If you need that, you could try replacing only the last occurrence.
More advanced versions might be able to handle indented code, checking whether code is part of a string or a comment, and other features, but in the end, to do this reliably you would have to parse the language structure, which in Bash is not easy.