Currently, I'm trying to automate a procedure we use at work. Whenever we install Oracle's JDK, we need to manually add it to our global PATH
variable. Here's an excerpt from the procedure :
sudo vi /etc/environment
add this at the beginning of the PATH : "/opt/jdk1.6.0_45/bin:"
Here is the content of /etc/environment
on my computer :
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
Here is what it will look like after its modification :
PATH="/opt/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
Do not forget that this file is not a script, but rather a file containing KEY=VALUES. This file stores the system-wide locale and path settings.
My question is how can I add a new path to the PATH
variable from /etc/environment
without involving any manual operation, preferably using only a bash script. Additionnaly, I would like to avoid seeing my JDK path added more than once if I run the resulting script twice.
You can do this using sed to first delete and then insert the jdk path:
#!/bin/bash
sed -e 's|/opt/jdk1.6.0_45/bin:||g' -i /etc/environment
sed -e 's|PATH="\(.*\)"|PATH="/opt/jdk1.6.0_45/bin:\1"|g' -i /etc/environment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With