在编辑.bashrc文件后出现了“语法错误,附近有意外的标记”

我正在尝试访问剪贴板,但当我在终端中输入source ~/.bashrc时,出现以下错误:
bash: /home/taran/.bashrc: line 2: syntax error near unexpected token ('
bash: /home/taran/.bashrc: line 2:alias pbpaste='xclip -selection 
clipboard -o'# ~/.bashrc: executed by bash(1) for non-login shells

我尝试按照Gary Woodfine's answerCommand Line Clipboard Access的教程进行操作。 cat ~/.bashrc 的输出结果是:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'# ~/.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-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
    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

# 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

这是在Ubuntu 19.04上的情况。有人能帮我弄清楚如何解决这个问题吗?
2个回答

mook765完全正确关于问题的原因,那个答案中提出的解决方案修复了语法错误,但我建议您以不同的方式解决它。

将别名定义放在.bashrc中是可以的,但最好不要把它们或任何东西放在该文件的顶部。

我们倾向于认为.bashrc只被交互式shell调用,但事实并非如此。非交互式远程shell(如果bash将其识别为这样)也会调用.bashrc这就是为什么 Ubuntu 的默认 .bashrc1 中包含此代码:2

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

基本上,你在.bashrc中放置的所有内容,包括但不限于别名定义,都应该放在那之下的某个位置。除非你有明确的理由这样做(这种情况很少见),否则你只能将自己的代码放在那段代码之上。 你可以将别名定义放在那段代码之下的任何位置,尽管我建议将它们放在文件的最末尾。或者你可能更喜欢将它们放在文件中一些现有别名定义的附近。或者你可能更喜欢将它们放在单独的文件~/.bash_aliases中,如果该文件不存在,你可以创建它。3 这些选择都是可以的。
这是一个更常见的例子,说明将自己的代码放在交互性检查之上可能产生奇怪和意外的效果。特定的问题发生在代码产生输出时,而别名定义不应该产生输出。(当使用别名时,它可能会扩展为产生输出的命令,但从语法上讲,别名定义不应该产生输出,除非将-p选项传递给alias命令。)我并不指望别名定义通常会引起问题,即使它们在非交互式shell中运行。非交互式shell默认情况下不执行别名扩展(尽管这只是一个默认设置)。然而,如果它们最终产生了意外的效果,很可能没有人会想到去检查这一点。
诚然,这只是一个避免将别名定义放在.bashrc文件的交互性检查之上的一个薄弱理由。然而,由于与将别名定义放在文件的其他任何位置相比,这样做没有任何好处,我建议遵循一般的方法,只将你有意在非交互式远程shell中运行的代码放在该检查之上。
另一个有趣的方面是,为什么这会成为语法错误:
alias pbpaste='xclip -selection clipboard -o'# ~/.bashrc: executed by bash(1) for non-login shells.

# 开始注释,可以跟随命令。然而,当 # 字符出现在一个较长的单词中时,并不具有开始注释的效果,除非它是该单词的第一个字符。(在这个意义上,“单词”包括像 pbpaste='xclip -selection clipboard -o'# 这样的内容,由于引用。)下面的文本原本是作为注释的,但被解析为 alias 内置命令的附加参数。但是,由于意外出现了 (,这在 shell 中具有特殊含义,但在这种情况下没有意义,因此解析它们时会发生错误。结果是 alias 内置命令实际上从未运行过,而你会得到一个语法错误。

因此,通过在那一行的 '# 字符之间加入一个空格,实际上可以修复语法错误。但如上所述,我建议进一步将别名定义移到文件的更低位置。


1Ubuntu中默认的.bashrc文件可以在/etc/skel/.bashrc路径下查看,前提是您尚未修改该文件。当创建用户时,该文件会被复制到用户的主目录中。与Ubuntu派生自的Debian发行版相比,这个文件几乎没有改动。本帖中的建议适用于Debian和Ubuntu中的Bash,但不一定适用于所有GNU/Linux系统的Bash。

2虽然很少见, 但也有可能启动非交互式登录shell。与交互式登录shell一样,这样的shell会自动加载~/.profile文件,并且Ubuntu中的默认~./profile明确加载~/.bashrc。将对~/.bashrc的添加放在非交互性检查之后,除了防止在非交互式远程shell中意外执行外,还可以防止其在非交互式登录shell的奇怪情况下被意外执行。

3Ubuntu的默认.bashrc检查是否存在~/.bash_aliases[ -f ~/.bash_aliases ]),并在存在时将其加载(. ~/.bash_aliases)。您发布的代码验证了您修改的.bashrc文件确实执行了这些操作--看起来唯一的修改是您在顶部添加的代码。


那个答案回答了我所有的问题,太棒了(也许应该提到.bash_aliases预计从.bashrc中被引用)。 - eckes
@EliahKagan,实际上,我没有注意到中间那句话,糟糕。这个答案强调了别名的位置,使得它读起来好像比实际情况更严重。如果,例如,别名也应用于非交互式shell,那将是一个更大的问题... 我确实理解你关于首先保持守卫条件的观点,但我们似乎对这些不同问题的优先顺序有不同意见。 ;) - ilkkachu
@ilkkachu 是的,我们可能对此有不同意见。另一方面,我在mook765已经发布并且OP已经将其标记为接受答案之后才开始撰写这个回答。因此,我决定在回答的开头提到那个答案("mook765是完全正确的,关于问题的原因,以及该答案中提出的解决方案修复了语法错误"),然后在接下来的大部分回答中讨论将它们放在文件的非常顶部以外的其他位置的替代解决方案。 - Eliah Kagan
2@eckes 谢谢你的建议 - 我已经添加了一些尾注来涵盖那个问题和一些相关问题,供那些感兴趣的读者参考。(在这个背景下,我认为不将~/.bashrc源于~/.bash_aliases视为一个特别重要的观点的原因是,检查OP的~/.bashrc文件后发现执行此操作的代码保持完整。然而,这确实是相关且有趣的,你提到它应该被提及是正确的。) - Eliah Kagan

警告在第二行:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'# ~/.bashrc: executed by bash(1) for non-login shells.

应该是这样:

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
# ~/.bashrc: executed by bash(1) for non-login shells.

看起来你在输入第二个别名后忘记按下 Enter 键,导致在同一行中在你的 alias 定义之后直接跟着 # ~/.bash...。没有前置空格,# ~/.bash... 无法被 shell 解释为注释,而被视为 alias 命令参数的一部分。

我还建议将别名放在文件 ~/.bash_aliases 中,当执行 ~/.bashrc 时会自动加载该文件,这样你就不需要编辑 ~/.bashrc 并可能弄乱它。

如果坚持将别名放在 ~/.bashrc 中,请将它们添加到文件末尾。

要深入了解此主题,请参考 Eliah 的出色回答


6这个答案如果你解释一下修复原理的话会更好。 - Andy
谢谢!顺便说一下,即使有人选择不遵循我的建议,在交互性检查之后将别名放在某个地方,我建议保留# ~/.bashrc: executed by bash(1) for non-login shells.作为第一行。没有技术原因要求它首先出现(或者根本不出现)。但这是对整个文件的注释文档化。所以对于人类读者来说,它在其他代码之后出现会相当困惑。我理解如果你不想改变这一点,特别是因为提问者接受了这个答案。(我认为在这种情况下,为此进行编辑或者只是保留它都是合理的。) - Eliah Kagan
"preseed" -- 你是不是指的是 "precede"? - Michael Harvey