通过 ~/.bash_profile 在 bash 中将目录添加到我的路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15508892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:54:35  来源:igfitidea点击:

Adding directory to my path through ~/.bash_profile in bash?

bashshellpath.bash-profile

提问by chepner

I want to add a directory to my PATH through the ~/.bash_profile file, though I don't have a file like this in my Ubuntu. I have the following code, which will check whether people have that file or not, and if not will create one and add all the existing path directories and the one entered by the user.

我想通过 ~/.bash_profile 文件向我的 PATH 添加一个目录,尽管我的 Ubuntu 中没有这样的文件。我有以下代码,它将检查人们是否拥有该文件,如果没有,将创建一个并添加所有现有路径目录和用户输入的目录。

#!/bin/bash

echo "Please enter a directory that you want to add to your PATH: "
read dr

if [ -f ~/.bash_profile ]
then
    # Add the read 'dr' directory to the end of the line that starts 
    # with PATH=, in your ~/.bash_profile file
    source ~/.bash_profile
else
    echo "PATH=$PATH:$dr" > ~/.bash_profile
    echo "export PATH" >> ~/.bash_profile
    source ~/.bash_profile
fi

The point is that, if the file already exists, I don't know how can I check to find the line that starts with PATH=, and then add the directory entered by the user to the end of that line. Please note that this is just an exercise given to me. I should add a directory entered by the user to the PATH through ~/.bash_profile. Though, I don't know why someone should bother to use ~/.bash_profile to add new directory to the PATH.

关键是,如果文件已经存在,我不知道如何检查以找到以PATH =开头的行,然后将用户输入的目录添加到该行的末尾。请注意,这只是给我的练习。我应该通过 ~/.bash_profile 将用户输入的目录添加到 PATH 中。不过,我不知道为什么有人要费心使用 ~/.bash_profile 将新目录添加到 PATH。

回答by chepner

Look at the lines you are adding to a new .bash_profile:

查看您添加到 new 的行.bash_profile

echo "PATH=$PATH:$dr" > ~/.bash_profile
echo "export PATH" >> ~/.bash_profile

The first one already adds $drto the PATH, regardless of what is already in PATH. This means you can do the same thing even if .bash_profilealready exists: just tack on a new line that appends yet another directory:

第一个已经添加$dr到 中PATH,无论PATH. 这意味着即使.bash_profile已经存在,您也可以做同样的事情:只需添加一个新行,附加另一个目录:

touch ~/.bash_profile
echo "export PATH=$PATH:$dr" >> ~/.bash_profile

A few things to note:

需要注意的几点:

  1. Ensure that .bash_profileexists by touching it; then you don't need to test for its existence.

  2. Only one line is needed; you can set a variable when you use exportto mark it for export to the environment.

  3. Escape the $in $PATHso that a literal dollar sign is written to .bash_profile; the same is not needed for $drbecause you want to let that expand so the proper directory is added.

  4. Append this new line using >>, so you don't overwrite the existing file.

  1. .bash_profile通过touching确保存在;那么你不需要测试它的存在。

  2. 只需要一行;您可以在export用于标记它以导出到环境时设置一个变量。

  3. 转义$in$PATH以便将文字美元符号写入.bash_profile; 不需要相同的内容,$dr因为您想让它扩展以便添加正确的目录。

  4. 使用 追加此新行>>,这样您就不会覆盖现有文件。

回答by user2032566

~/.bashrc 

Try this file in home.

在家里试试这个文件。