pathmunge /etc/profile

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

把参数添加进去环境变量,如果已经存在则不添加

pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}

这个函数不知道是什么时候的版本的,不过新版本的函数效率更高

#我的环境变量是/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/test/bin
pathmunge () {
    case ":${PATH}:" in
        :/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/test/bin:)
            echo "就是在环境变量前后添加:";;
        #不是正则表达式 任意字符:参数1:任意字符
        *:"$1":*)
            echo "true";;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

pathmunge
原文地址:https://www.cnblogs.com/ghgyj/p/4044758.html