Skip to main content
Update from comments, remove +, restore OP append method
Source Link
Stephen Rauch
  • 4.3k
  • 15
  • 24
  • 33

The second command has dropped the " characters from the replacement string. And I don't understand what you are doing with the s at the end, so how about:

sed '/export +JAVA_HOME=\{1,\}JAVA_HOME=/{h;s#=.*#="'$java'"#};$aexport;${x;/^$/{s##export JAVA_HOME="'$java'"'JAVA_HOME="'$java'"#;H};x}' -i ~/.bashrc 

The second command has dropped the " characters from the replacement string. And I don't understand what you are doing with the s at the end, so how about:

sed '/export +JAVA_HOME=/{h;s#=.*#="'$java'"#};$aexport JAVA_HOME="'$java'"' -i ~/.bashrc 

The second command has dropped the " characters from the replacement string. And I don't understand what you are doing with the s at the end, so how about:

sed '/export \{1,\}JAVA_HOME=/{h;s#=.*#="'$java'"#};${x;/^$/{s##export JAVA_HOME="'$java'"#;H};x}' -i ~/.bashrc 
Simplify append
Source Link
Stephen Rauch
  • 4.3k
  • 15
  • 24
  • 33

The second command has dropped the " characters from the replacement string.

How And I don't understand what you are doing with the s at the end, so how about:

sed '/export[export ]JAVA_HOME=+JAVA_HOME=/{h;s#=.*#="'$java'"#};${x;/^$/{s##export;$aexport JAVA_HOME="'$java'"#;H};x}'JAVA_HOME="'$java'"' -i ~/.bashrc 

The second command has dropped the " characters from the replacement string.

How about:

sed '/export[ ]JAVA_HOME=/{h;s#=.*#="'$java'"#};${x;/^$/{s##export JAVA_HOME="'$java'"#;H};x}' -i ~/.bashrc 

The second command has dropped the " characters from the replacement string. And I don't understand what you are doing with the s at the end, so how about:

sed '/export +JAVA_HOME=/{h;s#=.*#="'$java'"#};$aexport JAVA_HOME="'$java'"' -i ~/.bashrc 
Source Link
Stephen Rauch
  • 4.3k
  • 15
  • 24
  • 33

The second command has dropped the " characters from the replacement string.

How about:

sed '/export[ ]JAVA_HOME=/{h;s#=.*#="'$java'"#};${x;/^$/{s##export JAVA_HOME="'$java'"#;H};x}' -i ~/.bashrc