为什么我不能在PS1中使用$(...)而不是反引号? [英] Why can't I use $(...) in PS1 instead of backticks?

查看:46
本文介绍了为什么我不能在PS1中使用$(...)而不是反引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的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$: '  

是的,这很糟,但是对我来说很好-我的提示如下:

Yes, it's a mess, but it serves me well - my prompts look like this:

P2759474@RVPTINTCL415MQC 2017-10-06,11:20:18 ~/repos/jdk (master)  

它们甚至用颜色编码,其中user @ machine为绿色,timestamp为紫色,当前位置为黄色,任何git分支为蓝色.我有点生气,我必须使用反引号而不是 $()构造.

They are even color coded, with user@machine in green, timestamp in purple, current location in yellow, and any git branch in blue. I'm just a little annoyed that I have to use backticks instead of a $() construct.

有人知道为什么吗?愿意帮助我理解吗?这只是使用subshel​​l命令解析复杂的提示值时的一个问题,而这只是一个问题,因为我想了解它在那里的重要性...我们总是欢迎一般的改进建议.

Anyone know why? Willing to help me understand it? It's only a problem when parsing complex prompt values with subshell commands, and only a problem then because I want to understand why it matters there... General improvement suggestions always welcome while we're at it.

更新-

当前,当我尝试使用$()时,会得到很多

Currently when I try to use $() I get a lot of

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;

这告诉了我一些东西,也许...谢谢!

That tells me something, maybe... Thanks!

推荐答案

当您开始尝试在提示符中嵌入命令时,是时候开始使用 PROMPT_COMMAND 了.

When you start trying to embed commands in your prompt, it is time to start using 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'

这篇关于为什么我不能在PS1中使用$(...)而不是反引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆