【问题标题】:CENTOS 7 - $PATH incorrect after installing jdkCENTOS 7 - 安装 jdk 后 $PATH 不正确
【发布时间】:2026-01-16 16:25:01
【问题描述】:

我是 Linux 操作系统的新手,我在 centos7 上安装了 jdk。

现在每次登录时,我都会收到以下错误消息:

/usr/libexec/grepconf.sh: line 5: grep: command not found
-bash: /home/centos/.bash_profile: line 13: syntax error near unexpected token `newline'
-bash: /home/centos/.bash_profile: line 13: `export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep>'

所有命令都“未找到”

我发现这篇文章更新路径: https://unix.stackexchange.com/questions/302743/centos-7-all-command-not-found-except-cd

我用这个命令更新路径:

PATH=/usr/local/bin:/usr/bin:bin

所以现在有了 sudo nano /home/centos/ .bash_profile 命令我在 bash_profile 文件中有以下内容:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

 # User specific environment and startup programs

 PATH=$PATH:$HOME/.local/bin:$HOME/bin

 export PATH
 export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep>

什么是问题?我该如何解决所描述的错误?

第一次编辑以回答@航海问题:

这里是 .bash_profile 文件的内容:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep

这里是 grepconf 的内容:

#!/bin/sh

case "$1" in
    -c | --interactive-color)
        ! grep -qsi "^COLOR.*none" /etc/GREP_COLORS
        ;;
    *)
        echo >&2 "Invalid / no option passed, so far only -c | --interactive-color is supported."
        exit 1
        ;;
esac

第二次编辑以回答评论中的问题:

这里 ldd /usr/bin/grep 命令输出:

linux-vdso.so.1 =>  (0x00007fff769b8000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f0454df3000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f0454a26000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0454809000)
    /lib64/ld-linux-x86-64.so.2 (0x000055ce09e0c000)

这里是 ls -l /lib64/ld* 的结果

[centos@serveur-1 ~]$ ls -l /lib64/ld*
-rwxr-xr-x 1 root root 163400 Jan 29 18:55 /lib64/ld-2.17.so
lrwxrwxrwx 1 root root     10 Feb 20 21:51 /lib64/ld-linux-x86-64.so.2 -> ld-2.17.so

每次我退出,然后我登录, 我总是得到“找不到命令”,除了一些命令,例如:

java -version

所以每次我必须输入“PATH=/usr/local/bin:/usr/bin:bin/”(在这个网站上找到答案)并且每次我输入 echo $PATH 时,输出都会越来越复杂:

[centos@serveur-1 ~]$ echo $PATH
$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep

我想 $PATH 内容在登录后就不好了。

那我该如何彻底解决呢?

第三次编辑 @tripleee : 这里.bashrc文件内容:

[centos@serveur-1 ~]$ sudo nano /home/centos/.bashrc
  GNU nano 2.3.1                  File: /home/centos/.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

它谈到/etc/bashrc,所以这是它的内容:

[centos@serveur-1 ~]$ sudo nano /etc/bashrc
  GNU nano 2.3.1                   File: /etc/bashrc

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOM$
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOM$
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-de$
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }

    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; 

then
       umask 002
    else
       umask 022
    fi

    SHELL=/bin/bash
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
# vim:ts=4:sw=4

如果你来自“Windows 世界”,理解这个机制是相当困难的......

【问题讨论】:

  • PATH=/usr/local/bin:/usr/bin:bin -- 这是错误的,它在最后的bin 之前缺少一个斜线;但这都不能准确解释您的症状。登录时最后的PATH 是什么?
  • 运行grepconf 的结果是什么? .bashrc 中是否有您没有显示的命令?
  • 显示所有 .bash_profile 文件内容
  • 是的,但它也尝试运行.bashrc,并且您显示的代码没有运行grepconf;所以这是目前无法重现的,并且在某处有一些代码您没有向我们展示。你能再看看吗?您的主目录中是否有一个名为 .bashrc 的文件,如果有,它包含什么?
  • 缺少斜线的修复方法是 /bin 而不是 bin/

标签: bash path centos environment-variables


【解决方案1】:

通过更新 .bash_profile 解决如下:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs 


PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/bin:/usr/bin:/bin:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin

export PATH

我是 Centos 的新手,所以我希望我能找回我的 2 分(被反对票删除):

  • 为了回答 cmets 中提出的问题,我已对问题进行了编辑和结构化
  • 我一直在寻找答案(请参阅链接),但我不理解所有说明,因此可能很难说清楚

【讨论】: