如何在shell执行退出后添加$ PATH

时间:2014-09-03 05:17:56

标签: shell

我想在install.sh的特定目录中安装一些工具,并在test.sh中调用它们,因此我添加如下路径:

# part of install.sh
path_add()
{
    if [ -d "$1" ] && [ ":$PATH:" != *":$1:"* ]; then
        echo "add $1 to PATH"
        export PATH="$PATH:$1"
        echo $PATH
    else
        echo "$1 already existing in PATH"
    fi
}

echo表示$1已添加到$PATH,但当install.sh退出时,$PATH不包含刚刚添加的指定路径,如何永久添加它可以影响持续的shell环境?

1 个答案:

答案 0 :(得分:0)

# part of install.sh
path_add()
{
    if [ -d "$1" ] && [ ":$PATH:" != *":$1:"* ]; then
        echo "add $1 to PATH"

        #export PATH="$PATH:$1"
        # global
        echo "export PATH=$PATH:$1" >> /etc/profile
        # or local
        # echo "export PATH=$PATH:$1" >> ~/.profile

        echo $PATH
    else
        echo "$1 already existing in PATH"
    fi
}

/ etc / profile - 全局系统设置

〜/ .profile - 按用户设置