我在ubuntu中找不到.bash_profile

我在/home/user目录下的Ubuntu 14.04中找不到.bash_profile 。 我使用ls -a命令查看.bash_profile ,但是没有这样的文件。

Ubuntu使用~/.profile

你可以在Ubuntu中创建.bash_profile ,但是不会读取.profile

如果我们阅读.profile内容:

 cat ~/.profile 

产量

 # ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. 

所以使用~/.profile而不是~/.bash_profile

这意味着该文件不存在。 但是,如果将bash作为登录shell调用,则可以创建文件并使用bash执行/获取文件。 所以你需要通过shell登录(例如通过ssh )。

如果您希望每次打开终端时都执行内容,那么您应该修改.bashrc文件。

在调用登录shell时,bash将按以下顺序查找其配置文件:

 [0] ~/.bash_profile [1] ~/.bash_login [2] ~/.profile 

在找到第一个之后,它停止寻找其他人,所以如果我的$HOME bash中有.bash_profile将不再查找.bash_login.profile

从这三个文件名中,Ubuntu默认使用.profile ,如果您愿意,可以将其重命名为.bash_profile

 mv ~/.profile ~/.bash_profile 

现在,如果我们使用bash -lsu - $USERsudo -u $USER -i或任何其他运行bash作为登录shell的命令打开一个新的bash shell, ~/.bash_profile将被获取。

重要提示:

到目前为止我所谈到的仅适用于Bash本身,当您从GUI登录系统时,显示管理器负责获取正确的文件。

Ubuntu使用gdm3作为它的显示管理器,如果我们看看: /etc/gdm3/Xsession我们可以看到除了以下内容之外没有任何文件可以获取: .profile

 # First read /etc/profile and .profile for file in /etc/profile "$HOME/.profile"; do if [ -f "$file" ]; then source_with_error_check "$file" fi done 

因此,如果您使用GUI进行登录,请将文件保存在.profile名称下,否则您可能会错过环境中的一些变量和设置。

我想更好的选择是创建.profile的符号链接:

 ln -s ~/.profile ~/.bash_profile 

现在你的数据存在于.profilegdm不会遗漏任何内容,bash加载.bash_profile实际上是.profile ,并通过编辑它们得到相同的结果。

缺少.profile?

如果你没有.profile ,那么从这里获取它的副本:

 cp /etc/skel/.profile ~/.profile 

要么

 # Remember the note above cp /etc/skel/.profile ~/.bash_profile 

如果您的意思是.bashrc,您将在主文件夹中找到它。 如果不存在,可以将其从/ etc / skel文件夹复制到主文件夹。

如果您需要有关此主题的更多信息,请访问stefaan lippens页面。

http://stefaanlippens.net/bashrc_and_others

使用~/.profile代替~/.bash_profile最佳答案对我不起作用。

修改.bashrc工作

只是:

 vim ~/.bashrc 

注意:我正在使用Ubuntu WSL。