How do I manipulate $PATH elements in shell scripts?

前端 未结 12 1097
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:03

Is there a idiomatic way of removing elements from PATH-like shell variables?

That is I want to take

PATH=/home/joe/bin:/usr/local/bin:/usr/bin:/bin:         


        
12条回答
  •  清酒与你
    2020-11-28 04:50

    In line with dj_segfault's answer, I do this in scripts that append/prepend environment variables that might be executed multiple times:

    ld_library_path=${ORACLE_HOME}/lib
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH//${ld_library_path}?(:)/}
    export LD_LIBRARY_PATH=${ld_library_path}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    

    Using this same technique to remove, replace or manipulate entries in PATH is trivial given the filename-expansion-like pattern matching and pattern-list support of shell parameter expansion.

提交回复
热议问题