Created
February 24, 2020 17:43
-
-
Save prjemian/d1dd5fcb1dc97b017500c235360ebb66 to your computer and use it in GitHub Desktop.
bash: don't add same directory to PATH more than once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # bash shell script | |
| # don't add same directory to PATH more than once | |
| # | |
| # EXAMPLES: | |
| # _bin=${HOME}/bin | |
| # if [ -d "${_bin}" ] | |
| # then prepend_path ${_bin} | |
| # fi | |
| # | |
| # append_path ${EPICS_BASE_BIN} | |
| # append_path ${EPICS_BASE_LIB} | |
| _search_in_path_ () { | |
| if [ "$#" != "1" ]; then | |
| echo "usage: _search_in_path_ path" | |
| exit 1 | |
| fi | |
| path=$1 | |
| OIFS=$IFS | |
| IFS=":" | |
| found="" | |
| read -ra ITEMS <<< "$PATH" | |
| IFS=$OIFS | |
| for p in "${ITEMS[@]}"; do | |
| if [ "$p" == "$path" ]; then | |
| found+=1 | |
| fi | |
| done | |
| } | |
| append_path () { | |
| if [ "$#" != "1" ]; then | |
| echo "usage: append_path path" | |
| exit 1 | |
| fi | |
| _search_in_path_ $1 | |
| if [ "$found" == "" ]; then | |
| export PATH=${PATH}:${path} | |
| fi | |
| } | |
| prepend_path () { | |
| if [ "$#" != "1" ]; then | |
| echo "usage: prepend_path path" | |
| exit 1 | |
| fi | |
| _search_in_path_ $1 | |
| if [ "$found" == "" ]; then | |
| export PATH=${path}:${PATH} | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment