bash: ~/.bash_profile: 没有那个文件或目录。

19

我一直收到这个错误:

bash: ~/.bash_profile: No such file or directory

这是我的bash_profile:

alias ngrok=/Users/mmahalwy/Desktop/Code/ngrok

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

alias ls='ls -GFh'

function prompt {
  local BLACK="\[\033[0;30m\]"
  local BLACKBOLD="\[\033[1;30m\]"
  local RED="\[\033[0;31m\]"
  local REDBOLD="\[\033[1;31m\]"
  local GREEN="\[\033[0;32m\]"
  local GREENBOLD="\[\033[1;32m\]"
  local YELLOW="\[\033[0;33m\]"
  local YELLOWBOLD="\[\033[1;33m\]"
  local BLUE="\[\033[0;34m\]"
  local BLUEBOLD="\[\033[1;34m\]"
  local PURPLE="\[\033[0;35m\]"
  local PURPLEBOLD="\[\033[1;35m\]"
  local CYAN="\[\033[0;36m\]"
  local CYANBOLD="\[\033[1;36m\]"
  local WHITE="\[\033[0;37m\]"
  local WHITEBOLD="\[\033[1;37m\]"
  local RESETCOLOR="\[\e[00m\]"

  export PS1="\n$RED\u $PURPLE@ $GREEN\w $BLUE[\#] → $RESETCOLOR"
  export PS2="| → $RESETCOLOR"
}

# prompt

export PATH=/usr/local/bin:$PATH

source ~/.git-completion.bash

if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
fi


source ~/.profile
source /Users/mmahalwy/.iterm2_shell_integration.bash

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

设置 PS4=':$BASH_SOURCE:$LINENO+'; set -x 可以打印行号并进行复现。 - Charles Duffy
那么,我猜你在某个地方引用了一个需要取消引用才能扩展的 ~。(因此,在脚本中,我倾向于使用 $HOME 而不是 ~)。 - Charles Duffy
无论如何,如果您的问题不在这个特定文件中,我也不会感到惊讶,而是在尝试调用您的.bash_profile的其他某些东西中,但引用了用于这样做的~,从而错误地执行了此操作。 - Charles Duffy
另一种追踪此问题的方法:在您的shell中运行PS4=':$BASH_SOURCE:$LINENO+' bash -x -i,并查看错误消息上面的行的确切内容。 - Charles Duffy
顺便说一句,不要将自己的答案编辑到问题中,而是将其作为答案添加(如果您认为这仍然对其他人有用,考虑到它的回答方式;否则删除它可能是适当的)。 - Charles Duffy
当您遇到此错误时,您该怎么做? - frank.lin
3个回答

20

Bash使用~/.profile来代替~/.bash_profile

~/.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.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Bash将使用第一个可用的文件——如果没有.bash_profile,则会回退到.profile。当然,在这种情况下,.bash_profile明确地引用了.profile - Charles Duffy
root@debian:/home/mohsen/test# ls /etc/skel/ -a . .. .bash_logout .bashrc .profile - PersianGulf
但是要在您的“主目录”中运行。 - PersianGulf
1
这是不是意味着我没有bash?“-bash:/Users/scottlydon/.profile:没有那个文件或目录” - ScottyBlades
哦,你有Mac电脑。在Linux系统中,你可以在ls /etc/skel/ -a中找到它,然后你可以将它们复制到你的主目录中。但我不知道在Mac上它在哪里。@ScottyBlades - PersianGulf
这个答案应该被接受。 - Robin Bastiaan

5
这个问题是由于我错误地从vim中复制了一个结尾为"~/.bash_profile" 38L, 1070C的内容导致的。

0
在我的情况下,我收到了以下错误。
-bash: /Users/mac241/.rvm/scripts/rvm: No such file or directory
-bash: /Users/mac241/.rvm/scripts/rvm: No such file or directory

经过一些检查,我发现当我创建了.bash_profile文件时,下面两个命令已经预先写在文件中。
source /Users/mac241/.rvm/scripts/rvm
source /Users/mac241/.rvm/scripts/rvm  

根据错误,我知道这些是问题,并且在我删除它们之后,它开始工作了。希望您只编写并定义了那些需要在shell启动时自动运行的命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接