Bash

終端視窗總是將路徑從 .bashrc 出來如何修復?

  • October 20, 2020

系統:Linux Mint 20 (ubuntu)

因此,前幾天我為 C/C++ 安裝了 emscripten 的 wasm 編譯器,添加到我的 bashrc 的路徑似乎總是啟動一個新的終端 shell,告訴我路徑已添加並且終端上的使用者顏色已更改。

我可以通過這樣做來解決這個問題,source ~/.bashrc正常顏色又回來了,但我想知道這裡到底發生了什麼,是否有可能的解決方法?

以下是一些螢幕截圖,向您展示它的外觀:

做完 source ~/.bashrc

新的終端外殼

任何見解或提示將不勝感激!

編輯這些是您要查看的文件:

.bashrc:

   # ~/.bashrc: executed by bash(1) for non-login shells.
   # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
   # for examples

# If not running interactively, don't do anything
case $- in
   *i*) ;;
     *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
   debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
   xterm|xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
   if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
   # We have color support; assume it's compliant with Ecma-48
   # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
   # a case would tend to support setf rather than setaf.)
   color_prompt=yes
   else
   color_prompt=
   fi
fi

if [ "$color_prompt" = yes ]; then
   if [[ ${EUID} == 0 ]] ; then
       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
   else
       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] '
   fi
else
   PS1='${debian_chroot:+($debian_chroot)}\u@\h \w \$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
   PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h \w\a\]$PS1"
   ;;
*)
   ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
   test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
   alias ls='ls --color=auto'
   #alias dir='dir --color=auto'
   #alias vdir='vdir --color=auto'

   alias grep='grep --color=auto'
   alias fgrep='fgrep --color=auto'
   alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
   . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
 if [ -f /usr/share/bash-completion/bash_completion ]; then
   . /usr/share/bash-completion/bash_completion
 elif [ -f /etc/bash_completion ]; then
   . /etc/bash_completion
 fi
fi

if [ -x /usr/bin/mint-fortune ]; then
    /usr/bin/mint-fortune
fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
alias paper='. /home/doom/Desktop/Minecraft/Paper/paper'

export PATH=$PATH:"$USER/QT/5.15.1/gcc_64/bin"

export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/node/12.18.1_64bit/bin"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/upstream/emscripten"
source "/etc/profile.d/rvm.sh"
alias startPg='~/pgAdmin4/venv/bin/python ~/pgAdmin4/venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py'

和 /etc/bash.bashrc :

# System-wide .bashrc file for interactive bash(1) shells.

# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
   debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
# but only if not SUDOing and have SUDO_PS1 set; then assume smart user.
if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
#    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
#    ;;
#*)
#    ;;
#esac

# enable bash completion in interactive shells
#if ! shopt -oq posix; then
#  if [ -f /usr/share/bash-completion/bash_completion ]; then
#    . /usr/share/bash-completion/bash_completion
#  elif [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#  fi
#fi

# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ] && [ ! -e "$HOME/.hushlogin" ] ; then
   case " $(groups) " in *\ admin\ *|*\ sudo\ *)
   if [ -x /usr/bin/sudo ]; then
   cat <<-EOF
   To run a command as administrator (user "root"), use "sudo <command>".
   See "man sudo_root" for details.
   
   EOF
   fi
   esac
fi

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
   function command_not_found_handle {
           # check because c-n-f could've been removed in the meantime
               if [ -x /usr/lib/command-not-found ]; then
          /usr/lib/command-not-found -- "$1"
                  return $?
               elif [ -x /usr/share/command-not-found/command-not-found ]; then
          /usr/share/command-not-found/command-not-found -- "$1"
                  return $?
       else
          printf "%s: command not found\n" "$1" >&2
          return 127
       fi
   }
fi

編輯#2: 當我嘗試從 nvm 命令開始執行行時:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# This loads nvm命令行之後,nvm 命令現在可以工作了。

我的yarn命令在執行之前不起作用,但在執行之後起作用:

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

minecraft paper server 命令即使在以下情況下也不起作用:

alias paper='. /home/doom/Desktop/Minecraft/Paper/paper'

但是,該emcc命令在嘗試這些行之前和之後確實有效:

export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/node/12.18.1_64bit/bin"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/upstream/emscripten"

startPg命令在輸入之前不起作用,但在輸入後起作用:

alias startPg='~/pgAdmin4/venv/bin/python ~/pgAdmin4/venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py'

所有這些命令都在我做之後工作source ~/.bashrc

編輯#3 所以我得到了一些有趣的結果,這是我的 bashrc 文件:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

echo 'am i here?'

# If not running interactively, don't do anything
case $- in
   *i*) ;;
     *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
   debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
   xterm|xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
   if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
   # We have color support; assume it's compliant with Ecma-48
   # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
   # a case would tend to support setf rather than setaf.)
   color_prompt=yes
   else
   color_prompt=
   fi
fi

if [ "$color_prompt" = yes ]; then
   if [[ ${EUID} == 0 ]] ; then
       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
   else
       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] '
   fi
else
   PS1='${debian_chroot:+($debian_chroot)}\u@\h \w \$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
   PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h \w\a\]$PS1"
   ;;
*)
   ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
   test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
   alias ls='ls --color=auto'
   #alias dir='dir --color=auto'
   #alias vdir='vdir --color=auto'

   alias grep='grep --color=auto'
   alias fgrep='fgrep --color=auto'
   alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
   . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
 if [ -f /usr/share/bash-completion/bash_completion ]; then
   . /usr/share/bash-completion/bash_completion
 elif [ -f /etc/bash_completion ]; then
   . /etc/bash_completion
 fi
fi

if [ -x /usr/bin/mint-fortune ]; then
    /usr/bin/mint-fortune
fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
alias paper='. /home/doom/Desktop/Minecraft/Paper/paper'

export PATH=$PATH:"$USER/QT/5.15.1/gcc_64/bin"

echo 'hello world'

export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/node/12.18.1_64bit/bin"
export PATH=$PATH:"$HOME/doom/Desktop/RoseDevelopment/emsdk/upstream/emscripten"

echo 'hi'
# source "/etc/profile.d/rvm.sh"
alias startPg='~/pgAdmin4/venv/bin/python ~/pgAdmin4/venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py'

我已經註釋掉了rvm。在我這樣做之前和之後它仍然有效source ~/.bashrc我認為這是因為在安裝 rvm 時它會將您置於一個 rvm 組中,該組可以訪問我相信的命令。

emsdk (emcc) 路徑上方和下方的迴聲在我執行之前不會列印出來,source ~/.bashrc然後它們會執行。我還在文件的最頂部放置了一個回顯,並且在我執行文件源之前它也不會執行。

這是它的樣子:

新的一個

編輯#4 我很抱歉這越來越長,但我​​很想知道這個文件是否會導致問題:

.bash_profile

source "/home/doom/Desktop/RoseDevelopment/emsdk/emsdk_env.sh"

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

當我評論 rvm 行和 emsdk_env.sh 行時,迴聲消失了。rvm命令仍然有效, emcc 命令不再有效。在我這樣做之前,我仍然沒有顏色輸出source ~/.bashrc,然後我的大多數其他命令 nvm/yarn/paper/startPg 再次工作。

這讓我覺得我的 .bashrc 文件沒有在新的會話中載入?

這是一個外觀:

doom@doom-MacBookPro:~$ rvm
# It works here
doom@doom-MacBookPro:~$ yarn
-bash: yarn: command not found
doom@doom-MacBookPro:~$ rvm list
#This works here
doom@doom-MacBookPro:~$ emcc
-bash: emcc: command not found
doom@doom-MacBookPro:~$ paper
-bash: paper: command not found
doom@doom-MacBookPro:~$ nvm
-bash: nvm: command not found
doom@doom-MacBookPro:~$ source ~/.bashrc 
am i here?
hello world
hi
doom@doom-MacBookPro ~ $ nvm
#This works here
doom@doom-MacBookPro ~ $ paper
#This works here
doom@doom-MacBookPro ~ $ yarn
#This works here

好吧,我可能終於想通了。從我的問題下面的評論開始。順便謝謝@Criggie 和@msb。

run as command login shell如果我按照@user3584014 所做的作為對這個問題的第二個答案,看來我什至可以在我的終端首選項中重新打開:https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal https://askubuntu.com/a/768359/1138077

總結得很好:

In my case, simply the .bashrc loader lines were missing in .bash_profile

# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
   . "$HOME/.bashrc"
fi
I added it manually and it worked with my fresh login

這使我的所有命令(包括 emcc)都能正常工作,並從一開始就恢復我的顏色。

你好世界

Adding directories to PATH:
PATH += /home/doom/Desktop/RoseDevelopment/emsdk
PATH += /home/doom/Desktop/RoseDevelopment/emsdk/upstream/emscripten
PATH += /home/doom/Desktop/RoseDevelopment/emsdk/node/12.18.1_64bit/bin

Setting environment variables:
EMSDK = /home/doom/Desktop/RoseDevelopment/emsdk
EM_CONFIG = /home/doom/Desktop/RoseDevelopment/emsdk/.emscripten
EM_CACHE = /home/doom/Desktop/RoseDevelopment/emsdk/upstream/emscripten/cache
EMSDK_NODE = /home/doom/Desktop/RoseDevelopment/emsdk/node/12.18.1_64bit/bin/node
doom@doom-MacBookPro ~ $ emcc
emcc: error: no input files
doom@doom-MacBookPro ~ $ yarn -v
1.22.10
doom@doom-MacBookPro ~ $ rvm -v
Warning! PATH is not properly set up, /home/doom/.rvm/gems/ruby-2.5.3/bin is not at first place.
        Usually this is caused by shell initialization files. Search for PATH=... entries.
        You can also re-add RVM to your profile by running: rvm get stable --auto-dotfiles
        To fix it temporarily in this shell session run: rvm use ruby-2.5.3
        To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
rvm 1.29.10 (manual) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
doom@doom-MacBookPro ~ $ nvm -v
0.36.0
doom@doom-MacBookPro ~ $ 

我仍然收到來自我的來源的 emcc 命令迴聲

.bash_profile:
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
   . "$HOME/.bashrc"
fi

source "/home/doom/Desktop/RoseDevelopment/emsdk/emsdk_env.sh"

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

然而,這並不是什麼大問題,只要其他一切從一開始就可以正常工作,因為打開多個終端來處理事情,然後總是不得不做source ~/.bashrc才是真正的痛苦。

引用自:https://unix.stackexchange.com/questions/614742