我当前的 PS1:
PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]`date +%Y-%m-%d,%H:%M:%S` \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$: '
是的,这是一团糟,但它对我很有用——我的提示是这样的:
P2759474@RVPTINTCL415MQC 2017-10-06,11:20:18 ~/repos/jdk (master)
它们甚至用颜色编码,user@machine 为绿色,时间戳为紫色,当前位置为黄色,任何 git 分支为蓝色。我只是有点生气,因为我不得不使用反引号而不是$()
结构。
有人知道为什么吗?愿意帮我理解吗?这只是在使用子 shell 命令解析复杂的提示值时出现问题,并且只是因为我想了解为什么它在那里很重要......我们总是欢迎一般的改进建议。
更新-
目前,当我尝试使用 $() 时,我得到了很多
bash: command substitution: line 1: syntax error near unexpected token ')'
bash: command substitution: line 1: 'date +%Y-%m-%d,%H:%M:%S)'
bash: command substitution: line 1: syntax error near unexpected token ')'
bash: command substitution: line 1: '__git_ps1)'
我的环境有
BASH_VERSINFO=([0]="4" [1]="3" [2]="42" [3]="5" [4]="release" [5]="x86_64-pc-msys")
BASH_VERSION='4.3.42(5)-release'
[ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no;
这告诉了我一些事情,也许……谢谢!
最佳答案
当您开始尝试在提示中嵌入命令时,是时候开始使用 PROMPT_COMMAND
了。
# You won't even have to put the title-bar stuff in your prompt
# and there are already shortcuts for date and time
set_titlebar () {
printf '\033]0;%s:%s\007' "$TITLEPREFIX" "${PWD//[^[:ascii:]]/?}"
}
set_prompt () {
PS1='\[\033[32m\]\u@\h ' # user@host in green
PS1+='\[\033[35m\]\D{%Y-%m-%d},\t ' # Don't need date
PS1+='\[\033[33m\]\w\[\033[36m\]' # Directory in orange
PS1+=$(__git_ps1) # git info, if appropriate
PS1+='\[\033[0m\]\n$: '
}
PROMPT_COMMAND='set_titlebar;set_prompt'
关于linux - 为什么我不能在 PS1 中使用 $(...) 而不是反引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610384/