更改 .bash_profile 后,每个命令都失败并显示“找不到命令”?

Bla*_*man 55 command-line bash

我正在更新我的 .bash_profile,不幸的是我做了一些更新,现在我得到了:

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
-bash: dirname: command not found
-bash: /preexec.sh.lib: No such file or directory
-bash: preexec_install: command not found
-bash: sed: command not found
-bash: git: command not found
Run Code Online (Sandbox Code Playgroud)

我的 bash_profile 实际上引入了其他 .sh 文件(来源它们),所以我不确定是哪个修改可能导致了这种情况。

现在,如果我什至尝试查看文件列表,我会得到:

>ls
-bash: ls: command not found
-bash: sed: command not found
-bash: git: command not found
Run Code Online (Sandbox Code Playgroud)

关于如何跟踪错误源以及如何使用终端来处理诸如列出文件等基本内容的任何提示?

Jan*_*ary 74

在我看来,您有时会覆盖默认PATH环境变量。您遇到的错误类型表明PATH不包含/bin上述命令(包括bash)所在的位置。

例如,如果你这样做

PATH=/home/user/bin
Run Code Online (Sandbox Code Playgroud)

代替

PATH="$PATH":/home/user/bin
Run Code Online (Sandbox Code Playgroud)

  • 您必须在第二个 PATH 字符串中使用 '$' - `export PATH=$PATH:EC2_HOME/bin` (2认同)
  • 看,我们不知道您在做什么,您正在采购哪些其他文件,其中有什么。是的,每次获取文件时,都会将内容*附加*到您的路径中(这很好,因为通常 .bash_profile 只会读取一次)。一个一个地通过它,看看会发生什么。另外,EC2_HOME 不是变量吗?如果是,那么你应该在它前面加上一个 `$`,像这样: `export PATH=$PATH:$EC2_HOME/bin` (2认同)
  • 这是我的问题。为了重置我的 PATH 以便我可以进入我的 bash 配置文件并删除我的 `~/.bash_profile` 中的错误 PATH 行,我必须通过执行以下操作来重置我的 PATH:`PATH=/usr/local/bin:/usr/ bin:/bin:/usr/sbin:/sbin` (2认同)
  • 我记不清有多少次我陷入了这个问题,有多少次这个答案对我有很大帮助 (2认同)

K. *_*gor 24

开始调试 bash 脚本的一种方法是使用 -x 选项启动子shell:

$ bash --login -x
Run Code Online (Sandbox Code Playgroud)

这将向您显示在启动该 shell 时执行的每个命令及其参数。

指定 --login 选项是因为 .bash_profile 由登录 shell 读取。有关调试 bash 脚本的更多信息,请访问:http : //tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html。

最终,我认为 1 月的建议对您有用,但该链接值得一读以解决未来的问题。


小智 13

我可能已经发现了问题。它对我有用,它可能对你有用......

我的编辑器默认为 Windows (LF/CR) 保存。由于我同时使用这两个系统,这似乎是合乎逻辑的。当我需要弄乱我的 . 时.bash_profile,我在注释掉并尝试了没有任何效果的事情后意识到。我将保存更改为 OS X 格式(仅限 CR),瞧!终端中不再有“找不到命令”!

可能就是这么简单!


小智 6

在我无法重新运行之后。~/.bash_profile 或任何常用命令,如 whoami、grep 等。我想出了一种方法来重新导出所需的路径:

export PATH=/usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/cpanel/composer/bin:/usr/local/easy/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
Run Code Online (Sandbox Code Playgroud)

这应该适用于大多数系统,尽管其中一些路径并不存在于所有 Linux 软件包中。它对我有用。