in my .bash_profile file i want to update value of PATH variable. Also, want to add JAVA_PATH and JRE_HOME variables. And, I want to do all this with Shell Script .
I have no idea about how to do this with commands, so I overwrite the entire file using cat command-
cat >> ~/.bash_profile << _EOF_
#!/bin/bash/
if [ -f ~/.bashrc ]; then
.~/.bashrc
fi
#User specific environment and startup programs
JAVA_PATH=/usr/java/jdk1.6.0_35/bin
PATH=$JAVA_PATH:$PATH:HOME/bin
JRE_HOME=/usr/jdk1.6.0_35
export PATH
unset USERNAME
_EOF_
What would be the impact of doing so with this file? How i can easily update value of PATH variable and insert JAVA_PATH and JRE_HOME variables in this file?
You need to use some text manipulation tool - like sed or awk...
Here is simple example of how to change the PATH value in your .bash_profile
sed 's/^\([[:space:]]*PATH=\)\(.*\)$/\1"\/bin:\/usr\/bin:~\/bin"/' ~/.bash_profile
Notice the escaped special characters ( and /
It you want to overwrite your old file, you need to do it through temporary file like:
sed ... > /tmp/tmpbashprofile$$
mv /tmp/tmpbashprofile$$ ~/.bash_profile
Adding new settings is easy:
echo "JAVA_PATH=/usr/java/jdk1.6.0_35/bin" >> ~/.bash_profile
Notice the double >> - it appends data to specified file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.