I am scripting my new VM stack and I am trying to update or append JAVA_HOME in the ~/.bashrc file I've managed to do that by running the following sed command from the command line.sed '/export[ ]JAVA_HOME=/{h;s/=.*/='updatedJava'/};${x;/^$/{s//export JAVA_HOME='newJava'/;H};x}' -i ~/.bashrcHowever, when I try to run it from a script using a variable, I am getting the  following error: sed: -e expression #1, char 34: unknown option to `s'I figured out that it is caused due to fact that my path contain / which is confused with the sed delimiter, therefore I've changed it to this:  sed '/export[ ]JAVA_HOME=/{h;s#=.*#='$java'#};${x;/^$/{s##export JAVA_HOME='$java'#;H};x}' -i ~/.bashrcwhere $java contains a path to java, however now it doesn't work, it also doesn't throw any exception. Where did I make a mistake? 
Update or append JAVA_HOME with sed
sed;scripting;java;variable
Keeping your logic, I have recast the sed in a slightly compact form, wherein, the change to the JAVA_HOME is done once at the beginning of the sed.Plus, there's this bit about escaping the java variable so that it is fit to be included into the rhs of a s/// sed command.Plus, the sed is POSIX-ly.java_esc=$(printf '%s\n' $java | sed -e 's|[\&/]|\\&|g;$!s/$/\\/')sed -i.BAK -e '   1{      x      s/.*/export JAVA_HOME='$java_esc'/      x   }   # assuming the JAVA_HOME declaration does not span line boundaries   /^[[:blank:]]*export[[:blank:]]\{1,\}JAVA_HOME=/{      g      x      s/^/\/      x   }   ${      x      /^\n/!H      x   }' ~/.bashrc