tar*_*ran 11 command-line bash alias 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
Run Code Online (Sandbox Code Playgroud)
我尝试在Gary Woodfine's answer to Command 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
Run Code Online (Sandbox Code Playgroud)
这是在 Ubuntu 19.04 上。谁能帮我弄清楚如何解决这个问题?
Eli*_*gan 20
mook765关于问题的原因是完全正确的,并且该答案中提出的解决方案修复了语法错误,但我建议您以不同的方式解决它。
将别名定义放在 中很好.bashrc,但最好不要将它们——或任何东西——放在该文件的最顶部。
我们倾向于认为.bashrc仅由交互式 shell 提供源,但实际上并非如此。非交互式远程 shell(如果 bash 将它们识别为这样)也 source .bashrc. 这就是为什么Ubuntu 的默认.bashrc1包含以下代码:2
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Run Code Online (Sandbox Code Playgroud)
基本上你放入的所有东西.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.
Run Code Online (Sandbox Code Playgroud)
#开始注释,允许跟随命令。但是,当该#字符出现在较大的单词中时,它没有开始注释的效果,除非作为该单词的第一个字符。(从这个意义上说,“单词”包括诸如pbpaste='xclip -selection clipboard -o'#,由于引用。)以下文本旨在作为注释,被视为alias内置函数的附加参数。但是在解析它们时发生错误,这是由于意外的存在(,这对 shell 具有特殊意义,但在该上下文中没有意义。结果是alias内置函数实际上永远不会运行,而是出现语法错误。
因此,通过在该行的和字符之间放置一个空格,实际上可以使用一个字符的 edit 来修复语法错误。但如上所述,我建议更进一步,将文件中的别名定义移到更低的位置。'#
1.bashrc可以在 Ubuntu 中查看默认值/etc/skel/.bashrc,只要您没有修改该文件。创建用户时,这将复制到用户的主目录中。与 Ubuntu 中的许多文件一样,该文件与 Debian(Ubuntu 的衍生发行版)的改动很小。这篇文章中的建议适用于 Debian 和 Ubuntu 中的 Bash,但它不一定适用于所有 GNU/Linux 系统中的 Bash。
2也可以bash作为非交互式登录 shell启动,尽管很少见。像交互式登录 shell 一样,这样的 shell 会~/.profile自动获取源代码,并且默认~./profile在 Ubuntu 中显式使用~/.bashrc. 除了防止在非交互式远程 shell 中意外执行之外,将您的添加放在~/.bashrc交互检查下方还可以防止它在非交互式登录 shell 的奇怪情况下被无意执行。
3 Ubuntu 的默认.bashrc检查是否~/.bash_aliases存在 ([ -f ~/.bash_aliases ]),如果存在则获取它 (. ~/.bash_aliases)。您发布的代码验证了您修改后的.bashrc文件确实执行了这些操作——看起来唯一的变化是您在顶部添加的代码。
moo*_*765 17
警告在第二行:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'# ~/.bashrc: executed by bash(1) for non-login shells.
Run Code Online (Sandbox Code Playgroud)
那应该是:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
# ~/.bashrc: executed by bash(1) for non-login shells.
Run Code Online (Sandbox Code Playgroud)
看起来您Enter在输入第二个别名后忘记点击导致# ~/.bash...直接alias在同一行中跟随您的定义。没有前面的空格# ~/.bash...不能被 shell 解释为注释,而是作为alias命令参数的一部分。
我还建议将别名放置在执行~/.bash_aliases时将获取的文件中~/.bashrc,这样您就不需要编辑~/.bashrc并最终弄乱它。
如果您坚持在 中放置别名~/.bashrc,请将它们添加到文件末尾。
要更深入地了解该主题,请参阅Eliah对您的问题的出色回答。
| 归档时间: |
|
| 查看次数: |
12292 次 |
| 最近记录: |