我在我的 MacBook 上完美地設定了終端和提示,但是在設定我的 PC 時我遇到了一個問題,它不會顯示我當前的 git 分支并且我收到一個錯誤。任何幫助將不勝感激。
下面的文字是我的 ~/.bashrc & ~/.bash_profile
# colored text.
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
RED="\[\033[0;31m\]"
PURPLE="\[\033[0;35m\]"
BROWN="\[\033[0;33m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_CYAN="\[\033[1;36m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
BOLD=$(tput bold); # bold text
orange=$(tput setaf 166);
red=$(tput setaf 001);
blue=$(tput setaf 004);
pink=$(tput setaf 005);
teal=$(tput setaf 006);
green=$(tput setaf 71);
white=$(tput setaf 15);
reset="\[\033[0m\]" #0m restores to the terminal's default colour
# Display current git branch in terminal.
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# command prompt
PS1="\[${BOLD}\]\n"; # Display prompt text in BOLD & fresh line.
PS1 ="\[${blue}\]User:" # Display "User:" text.
PS1 ="\[${orange}\]\u "; # Display active user.
PS1 ="\[${blue}\]Host:"; # Display "Host:" text.
PS1 ="\[${orange}\]\h "; # Display active host.
PS1 ="\[${blue}\]Branch:" # Display "Branch:" text.
PS1 ="\[${orange}\]"'$(git_branch) ' # Call git_branch to be displayed.
PS1 ="\[${blue}\]Directory:"; # Display "Directory:" text.
PS1 ="\[${orange}\]\W "; # Display working directory path.
PS1 ="\n"; # Create a new line to write on.
PS1 ="\[${white}\]-> \[${reset}\]"; # Display "$" & Color reset.
# Export file to be used in terminal (source ~/.bashrc)
export PS1;
以下是我在“source ~/.bashrc”時遇到的錯誤
\bash: command substitution: line 1: syntax error near unexpected token )'
\bash: command substitution: line 1: \git_branch)'
uj5u.com熱心網友回復:
我更喜歡使用PROMPT_COMMAND每次收到提示時都會執行的 ,因此會相應地更新它。
進入的示例.bashrc:
tputps () {
echo -n '\['
tput "$@"
echo -n '\]'
}
prompt_builder () {
# username
tputps setaf 2
echo -n '\u'
# if I'm in a git repo, add some info from that:
commit=$(git rev-parse -q --short HEAD 2>/dev/null)
if [[ -n $commit ]]; then
tputps setaf 140
echo -n " $commit"
fi
branch=$(git branch --show-current 2>/dev/null)
if [[ -n $branch ]]; then
tputps setaf 10
echo -n " $branch"
fi
# directory
tputps setaf 208
echo -n ' \w'
tputps sgr0 0
}
prompt_cmd () {
PS1="$(prompt_builder) \$ "
}
export PROMPT_COMMAND=prompt_cmd
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323970.html
上一篇:如何使用ANSI顏色代碼將彩色文本從檔案列印到bash終端?
下一篇:在shell腳本中使用字串的條件`if`陳述句以不同的方式配置兩臺計算機(Ubuntu/Gnome與Debian/XCFE)
