In Bash, you only need to export the variable once, not at each assignment. Remove export from all of your PATH assignments then put export PATH
on a line by itself at the end. If you are just setting the variable once, like with CC, then export in front of it is fine. You could also export them using a single command such as with export PATH CC ANDROID_HOME
.
The distribution tarball for Bash comes with several convenience functions for prepending or appending a path to a variable. I think they are in a contrib file called "pathfuncs". They can make your rc file much more readable.
- add_path will add the entry to the end of the PATH variable
- pre_path will add the entry to the beginning of the PATH variable
- del_path will remove the entry from the PATH variable, wherever it is
If you specify a variable as the second argument, it will use that instead of PATH.
For convenience, here they are:
# is $1 missing from $2 (or PATH) ?
no_path() {
eval "case :\$${2-PATH}: in *:$1:*) return 1;; *) return 0;; esac"
}
# if $1 exists and is not in path, append it
add_path () {
[ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="\$${2:-PATH}:$1"
}
# if $1 exists and is not in path, prepend it
pre_path () {
[ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="$1:\$${2:-PATH}"
}
# if $1 is in path, remove it
del_path () {
no_path $* || eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: |
sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`
}
If you add those to your bash startup file, you can add to your PATH like this:
pre_path $HOME/bin
add_path /sbin
add_path /usr/sbin
Or specify a different variable:
pre_path $HOME/man MANPATH
pre_path $HOME/share/man MANPATH
add_path /usr/local/man MANPATH
add_path /usr/share/man MANPATH
I use this method in my rc files putting the pre_paths first and the add_paths second. It makes all of my path changes easy to understand at a glance. Another benefit is that the lines are short enough that I can add a trailing comment on a line if necessary.
And since these are functions, you can use them interactively from the commend line, such as by saying add_path `pwd`
to add the current directory to the path.
Edit: Fixing your supplied rc file with the functions above, I have this:
pre_path /opt/local/bin
pre_path /opt/local/sbin
pre_path ~/Downloads/android-ndk-r7
add_path ~/scripts
add_path ~/Downloads/android-sdk-mac_x86/platform-tools
add_path ~/Downloads/android-sdk-mac_x86/tools
add_path ~/Downloads/android-sdk-mac_x86/platform-tools/adb
add_path ~/bin
add_path ~/bin/subl
add_path ~/.rvm/gems/ruby-1.9.3-head/gems/git-media-0.1.1/bin
add_path $HOME/bin
add_path /Users/me/Downloads/android-sdk-mac_86/tools
add_path /Users/me/Downloads/android-sdk-mac_86/platform-tools
add_path /usr/local/bin
add_path /usr/bin
add_path /bin
add_path /usr/sbin
add_path /sbin
add_path /usr/X11/bin
add_path /.rvm/scripts/rvm
add_path /.rvm/scripts/rvm
add_path /~/Downloads/android-sdk-mac_x86/tools/android
add_path /~/Downloads/android-ndk-r7/
add_path /~/Downloads/android-sdk-mac_x86/platform-tools
ANDROID_HOME=~/Downloads/android-sdk-mac_x86
add_path $ANDROID_HOME/platform-tools
export PATH
export CC=gcc-4.2
You have some cleanup to do to remove duplicates and put things in a better order.
3I don't understand the question. Delete the lines you no longer need. Done. – Der Hochstapler – 2012-04-05T14:27:49.647