0

I am trying to set and edit permanently environment variables in bash script, adding export VAR="demo" to .bash_profile worked for set peramently purpose, but i am not able to edit this var,

the idea is to write a script to set or edit if exists in this file.

tried this code for now but didn't work out:

VARIABLE_FILE=/root/.bash_profile
if [ -f ${VARIABLE_FILE} ] ; then
   . ${VARIABLE_FILE}
   if [ ! -z "${USER_PROMPT}" ] ; then
      export USER_PROMPT="haloooo"
      echo "USER PROMPT : $USER_PROMPT"
      # Prints the correct value but doesn't update .bash_profile
   fi
fi
shx2
  • 61,779
  • 13
  • 130
  • 153
seismael
  • 77
  • 1
  • 10

1 Answers1

2

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.

Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • thanks for the detailed answer, i added a line to export this variable in /root/.bash_profile like JAVA_HOME that each time a user logs in it will set this variable, and if he wants to change then i wanted to change for current session and bash_profile, i thought before that export command saves the environment variable in some file and i wanted to edit for current session – seismael Dec 25 '13 at 10:55