如何将 .bashrc 恢复为默认设置?

我一直在尝试在Eclipse上安装Android开发工具,我按照这个视频中的信息进行操作。
就像视频中所说的那样,我将以下两行代码添加到了.bashrc文件中:
export PATH=$(PATH):-/android-sdk/tools/
export PATH=$(PATH):-/android-sdk/platform-tools/

然而,似乎这并没有帮助我在Eclipse上安装Android文件,因为我仍然遇到了问题。更重要的是,现在似乎我无法在终端中执行任何命令,总是出现一个严重错误!每次我尝试在终端中执行命令时,都会收到以下消息:
“由于'/usr/bin'未包含在PATH环境变量中,因此找不到该命令。”
有没有办法将.bashrc恢复到默认状态?

1根本原因:$(PATH) 实际上应该是 $PATH - Elder Geek
$ nano .bashrc - StressedBoi_69420
4个回答

想法:/etc/skel/中存在.bashrc.profile等的备份副本。因此,可以通过从那里覆盖来替换损坏的.bashrc

注意:如果用新的文件替换.bashrc文件,将会删除您对其进行的任何其他修改。例如,可以在.bashrc中添加别名、自定义函数路径。当您替换文件时,所有这些修改都将丢失。最好在替换之前保留您修改过的.bashrc的备份副本。稍后,您可以仔细提取所需部分。要在您的主目录中以my_bashrc命名保留已修改的.bashrc的备份副本,请在终端中使用以下命令:

/bin/cp ~/.bashrc ~/my_bashrc

为什么要使用/bin/cp:如果你在修改了~/.bashrc文件时弄乱了$PATH变量,那么所有可执行文件将无法从终端访问,cp命令也将无法工作。因此,在尝试复制受损的~/.bashrc文件时,建议使用完整路径/bin/cp来调用cp命令。
最后,在终端中使用以下命令来替换~/.bashrc文件为新的副本,
/bin/cp /etc/skel/.bashrc ~/

它将用一个全新的文件替换您损坏的~/.bashrc。之后,您需要在终端中加载~/.bashrc以使更改立即生效,请输入以下命令:

. ~/.bashrc

或者,
source ~/.bashrc

或者,如果那个没有效果的话,你可以关闭终端然后再打开它。

谢谢您的回复!不知为何,我仍然收到错误信息。 - nanananana
2请问具体的错误信息是什么? - sourav c.
命令 'cp' 可在 '/bin/cp' 中找到。 由于 '/bin' 未包含在 PATH 环境变量中,因此无法定位该命令。 cp: 找不到命令 - nanananana
echo $PATH 的输出是什么? - sourav c.
:-/android-sdk/platform-tools - nanananana
你除了.bashrc之外,还在哪里放置了那个export行呢? - sourav c.
我只把它放在.bashrc文件中。 - nanananana
请删除您的.bashrc文件中的这些行,或者按照上述步骤进行操作。它将替换您的.bashrc文件,并解决问题的末尾。 - sourav c.
无论如何不要忘记在任何情况下都要“source /home/$USER/.bashrc”。 - sourav c.
还有一件事你从一开始就错了。符号是波浪线 ~,而不是连字符。我之前忘了提到。在shell中,~等同于/home/$USER - sourav c.
谢谢!你的帮助起了本质作用;我最终只是不得不手动将.bashrc文件移动到我的主目录,因为终端无法工作。 - nanananana
4请注意,这是一种过度解决方案,将删除您对文件所做的任何其他修改。 - Braiam
source命令具体是做什么的?它在哪里使用? - juggernauthk108

如果无法获取有效的shell

通过文件浏览器

  • 打开文件浏览器,进入主目录,按下CtrlH以显示隐藏文件。根据需要编辑.bashrc
  • 打开文件浏览器,进入/etc/skel,按下CtrlH以显示隐藏文件。将.bashrc复制到您的主文件夹中,以恢复为默认设置。

通过运行菜单

  • 按下AltF2,输入gedit .bashrc,按下Enter。根据需要进行编辑。
  • 按下AltF2,使用命令/bin/cp /etc/skel/.bashrc ~/(参考souravc's answer)将其恢复为默认设置。

通过终端

  • 打开终端,并忽略你没有shell的事实。转到编辑首选项配置文件

    enter image description here

  • 要么创建一个新的配置文件,要么编辑当前的配置文件以更改命令:

    enter image description here

    使用/bin/bash --norc/bin/bash --rcfile=/etc/skel/.bashrc

  • 启动一个新的标签页(如果你创建了自定义配置文件,则使用该自定义配置文件)。根据需要使用工作的shell。
  • 完成后,删除自定义配置文件,或取消选择默认配置文件中的自定义命令选项。

如果你有SSH

运行SSH时使用自定义命令,这应该帮助你绕过.bashrc

ssh -t <host> dash

dash shell(也称为/bin/sh)非常简洁,但足以恢复.bashrc文件。

如果无法获取图形界面且没有SSH

进入恢复模式(如何进入恢复模式?),这将为您提供root shell。在/home目录中查找您的用户主目录。


通过TTY方式
你可以按下ctrl+alt+f1进入TTY,然后使用用户名和密码登录。之后,按下ctrl+alt+f7将会返回到图形用户界面。
现在,请将现有的原始.bashrc文件从/etc/skel复制到你的主目录中。
cp /etc/skel/.bashrc ~/

如果你不确定你的.profile是否也被更改了,甚至可以把剩下的部分也带过来。
cp /etc/skel/.profile ~/

现在要使.bashrc立即生效,您可能希望使用以下命令来源码它:
source ~/.bashrc

如果您替换了.profile文件,您需要重新启动计算机才能使更改生效。

这个解决方案对我来说很有效。谢谢 :) - Hemant Kumar

我尝试了上面的答案,但由于某种原因,我的/etc/skel/目录中没有正确的文件。
我在这个GitHub Gist by Mario Bonales上找到了默认的~/.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
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# 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

# 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-color) 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
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
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

# 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 [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

5如果你必须在线查找,请使用以下来源:https://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/bash/trusty/view/head:/debian/skel.bashrc - muru