bash .bashrc 中断,在 shell 中输入的第二行吃掉了第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5087036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.bashrc break, second line entered in shell eats up first line
提问by sent-hil
I've using a .bashrc from one of peepcode's screencast.
我使用了来自 peepcode 的截屏视频之一的 .bashrc。
# 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)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
# 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 "3]0;${USER}@${HOSTNAME}: ${PWD}grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR="\[3[0;31m\]"
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR="\[3[0;37m\]"
else
local COLOR="\[3[0;32m\]"
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m$"
# The __git_ps1 function inserts the current git branch where %s is
echo "$(__git_ps1 '(%s|')${SINCE_LAST_COMMIT}"
fi
}
7"'
# ;;
#*)
# ;;
#esac
# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi
# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ]; then
case " $(groups) " in *\ admin\ *)
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 ]; 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/bin/python /usr/lib/command-not-found --
return $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found --
return $?
else
return 127
fi
}
fi
. ~/bin/bash_colors.sh
#load bash aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Unbreak broken, non-colored terminal
export TERM='xterm-color'
alias ls='ls -G'
alias ll='ls -lG'
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
export GREP_OPTIONS="--color"
# Erase duplicates in history
export HISTCONTROL=erasedups
# Store 10k history entries
export HISTSIZE=10000
# Append to the history file when exiting instead of overwriting it
shopt -s histappend
function minutes_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1`
seconds_since_last_commit=$((now-last_commit))
minutes_since_last_commit=$((seconds_since_last_commit/60))
echo $minutes_since_last_commit
}
grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR=${RED}
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR=${YELLOW}
else
local COLOR=${GREEN}
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m${NORMAL}"
# The __git_ps1 function inserts the current git branch where %s is
local GIT_PROMPT=`__git_ps1 "(%s|${SINCE_LAST_COMMIT})"`
echo ${GIT_PROMPT}
fi
}
export PS1='$(grb_git_prompt)$ '
gd() { git diff $* | view -; }
gdc() { gd --cached $*; }
source ~/bin/git-completion.bash
I can confirm the error is in grb_git_promit. What happens is the second lines typed in shell is overwriting the first line. Can anyone could help me with this?
我可以确认错误在 grb_git_promit 中。发生的情况是在 shell 中输入的第二行覆盖了第一行。任何人都可以帮我解决这个问题吗?
EDIT:
编辑:
PS1='\[3[38;5;1m\]some red text\[3[0m\]$ '
回答by Paused until further notice.
When you have colors and other non-printing escape sequences in your prompt, you have to surround them with escaped single brackets. Here is a simple example:
当您的提示中有颜色和其他非打印转义序列时,您必须用转义的单括号将它们括起来。这是一个简单的例子:
red='3[38;5;1m'
none='3[0m'
PS1='\[$red\]some red text\[$none\]$ '
Which will cause the prompt to display "some red text" in red and the dollar sign (or "#") in the default color.
这将导致提示以红色显示“一些红色文本”,并以默认颜色显示美元符号(或“#”)。
Here is another way to do the same thing:
这是做同样事情的另一种方法:
red=$(tput setaf 1)
none=$(tput sgr0)
By the way, to do this more portably and have less complex build-up of variables:
顺便说一句,为了更便携地做到这一点,并减少复杂的变量构建:
##代码##See man 5 terminfofor more information on the settings.
有关man 5 terminfo设置的更多信息,请参阅。

