如何在 Mac OSX 上重置 Bash,.bash_profile 已损坏且 bash 不再有效

Str*_*one 19 terminal bash .bash-profile macos

我在运行最新版本的 Mountain Lion 的 MacBook Pro 上。

我真的需要一些帮助,我已经设法破坏了我的 .bash_profile(我认为),这样每次打开终端时都会出现下面列出的错误。

-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin': not a valid identifier
-bash: export: `/Users/rob/Applications/sbt/bin:': not a valid identifier
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
-bash: tar: command not found
-bash: grep: command not found
-bash: cat: command not found
-bash: find: command not found
Run Code Online (Sandbox Code Playgroud)

我不确定发生了什么,我没有 sudo、cd 或任何正常命令。我能够访问任何主目录的唯一方法是通过 finder 中的 go to folder 命令并尝试查找该文件但无济于事。

最重要的是,我想我创建了一个可能导致问题的文件,我想编辑 .bash_profile 所以我输入了

sudo nano ./bash_profile
Run Code Online (Sandbox Code Playgroud)

这会在 nano 中打开一个新文件,我认为该文件已被保存。在此之后,我打开了真正的 .bash_profile 以添加 node.js 的路径。

如果我可以访问 .bash_profile 我想我可以让它回到正轨但我找不到它,我应该重新安装 bash 吗?如果是这样,我将如何在 mac 上做到这一点,我尝试使用

brew install bash
Run Code Online (Sandbox Code Playgroud)

我得到

-bash: brew: command not found
Run Code Online (Sandbox Code Playgroud)

Lri*_*Lri 23

??. 在文件对话框中显示隐藏文件。您可以尝试使用 TextEdit 打开 ~/.bash_profile 并暂时将其设为空白。


Gja*_*lar 12

只需将我所有的评论放在一起以获得答案:

您应该做的第一件事是更改外壳,这样您就可以设置一个不会加载 bash init-scripts ( .bashrc, .bash_profile)的外壳- 可以在此处查看如何为 Mac OS X 终端应用程序执行此操作:Apple 支持

现在您应该能够再次打开终端并使用您喜欢的命令行编辑器打开.bash_profile文件(例如nanovi):

在此文件中,您必须重置PATH终端使用的变量以查找它可以执行的程序。

这可以通过以下两行来完成:

PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH" # Make sure to use double quotes not single quotes And on a new line 
export PATH
Run Code Online (Sandbox Code Playgroud)

这将首先设置您希望在 PATH 上拥有的目录,并导出此 PATH 以使其可用于从此 shell 启动的所有程序(通过导出)。


小智 5

我遇到了同样的问题,我只是找到了更简单的解决方案:

/usr/bin/nano .bash_profile

删除所有混乱的内容,然后保存文件