How do I manipulate $PATH elements in shell scripts?

前端 未结 12 1089
伪装坚强ぢ
伪装坚强ぢ 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:36

    I prefer using ruby to the likes of awk/sed/foo these days, so here's my approach to deal with dupes,

    # add it to the path
    PATH=~/bin/:$PATH:~/bin
    export PATH=$(ruby -e 'puts ENV["PATH"].split(/:/).uniq.join(":")')
    

    create a function for reuse,

    mungepath() {
       export PATH=$(ruby -e 'puts ENV["PATH"].split(/:/).uniq.join(":")')
    }
    

    Hash, arrays and strings in a ruby one liner :)

提交回复
热议问题