Bash 프롬프트는 새 프롬프트가 표시될 때마다 명령을 실행합니다.

Bash 프롬프트는 새 프롬프트가 표시될 때마다 명령을 실행합니다.

bash에 현재 git 브랜치를 보여주는 다음 프롬프트가 있습니다.

PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc

.bashrc를 얻을 때는 작동하지만 브랜치를 변경할 때는 작동하지 않습니다. 따라서 PS1 var는 .bashrc를 얻을 때만 평가되지만 새 프롬프트가 표시될 때마다 평가해야 합니다. Bash 4.3에서 이를 달성하는 방법은 무엇입니까?

답변1

지금은 프롬프트로 사용하여 문제를 해결했습니다.

PS1="$green\u $r@ $red\h $r: $yellow\W \!$r \$(git_prompt) \n$yellow\$ $r"

여러 문자열을 하나의 PS1 문자열에 연결하기 전에 문제가 있는 것 같습니다. 비결은 \명령을 실행하기 전에 작성하는 것 입니다 $(git_prompt).

그래서 $(git_prompt)평가할 때 평가를 받게 되고,.bashrc

\$(git_prompt)새 프롬프트가 표시될 때마다 평가됩니다.

답변2

귀하의 문제는 $(git_prompt)상수 문자열로 평가되는 것 입니다.앞으로에 추가되었습니다 $PS1. 코드를 추가해야 합니다.

PS1+='$(git_prompt)'

답변3

ps1에서 작은따옴표를 사용해 보세요

PS1+='$(git_prompt)'

나도 내꺼 추천해개인용 컴퓨터기능

psOne () 
{ 
    ps1tm=${1:-01};
    ps1tc=(30 31 32 33 34 35 36 37 38);
    PS1='${debian_chroot:+($debian_chroot)}\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]\u\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]@\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]\h\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] :\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] \w\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] \$ '
}

여기에 이미지 설명을 입력하세요.

답변4

뭔가 미친 걸 보고 싶나요? 이것이 bash 프롬프트를 작성하는 방법입니다.

# inspiration: http://www.stumbleupon.com/su/2LpQMi 
user_host_path="${debian_chroot:+($debian_chroot) }"'\u@\h:\w'
xterm_title='\[\e]0;'"$user_host_path"'\a\]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="${xterm_title}"
git_branch='$(git_current_branch " (%s)")'
line1="${line1}${user_host_path}${git_branch} "
line2='\$ '
print_time='{ printf "%*s" $(($(tput cols) - 10)) " "|sed -e "s/./˙/g" -re "s/.{6}(..)$/ bash \1/"; date "+ %T"; } >&2'
color_bold='\[\e[0;1m\]'
color_reset='\[\e[0m\]'
PROMPT_COMMAND="_rc_=\$?;${print_time};((_rc_!=0)) && PS1='${line1}\n${color_bold}[\$_rc_]${color_reset} ${line2}' || PS1='${line1}\n${line2}'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch

저는 색깔을 별로 좋아하지 않습니다.

관련 정보