5

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.

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

I'd combine those into a single invocation of sed. Commands can be joined with semicolons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.