错误PS1 GIT功能 - Gitbash与Cygwin之间的冲突

发布于 2025-02-11 15:12:58 字数 1499 浏览 0 评论 0原文

我正在收到以下错误,试图自定义我的PS1以获取GIT

错误:

bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `get_dir)'
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `get_branch)'
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `parse_git_dirty)'

我的.bashrc

#...
function get_dir() {
    [[ ${PWD} == $HOME ]] && echo '~' || echo ${PWD##*/}
}
function get_branch() {
    [[ -d .git ]] && echo "$(git branch --show-current)" 
}
#...

这很棘手!这首先要在我的Cygwin上完美工作,但是当我使用此语法“修复”时,在Gitbash上产生了这个错误

export PS1="${LIGHTGREEN}\t${NC} ${PINK}\$(get_dir)${NC}${LIGHTBLUE} \$(get_branch) ${NC}${RED}\$(parse_git_dirty)${NC} \n${GREEN}$ ${NC}"

,我没有错误,但没有动态工作。每次更改目录,

export PS1="${LIGHTGREEN}\t${NC} ${PINK}`get_dir`${NC}${LIGHTBLUE} `get_branch` ${NC}${RED}`parse_git_dirty`${NC} \n${GREEN}$ ${NC}"

即更改\ $(get_dir) get_dir

在Cygwin上导致:

I'm receiving the following error trying to customize my PS1 for git

Error:

bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `get_dir)'
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `get_branch)'
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `parse_git_dirty)'

My .bashrc

#...
function get_dir() {
    [[ ${PWD} == $HOME ]] && echo '~' || echo ${PWD##*/}
}
function get_branch() {
    [[ -d .git ]] && echo "$(git branch --show-current)" 
}
#...

Here's tricky! this first snipped worked perfectly on my Cygwin, but produce that error on Gitbash

export PS1="${LIGHTGREEN}\t${NC} ${PINK}\$(get_dir)${NC}${LIGHTBLUE} \$(get_branch) ${NC}${RED}\$(parse_git_dirty)${NC} \n${GREEN}$ ${NC}"

when I "fix it" using this syntax, I've got no error but it's NOT working dynamically. I Need to reload my .bashrc each time that I change the directory

export PS1="${LIGHTGREEN}\t${NC} ${PINK}`get_dir`${NC}${LIGHTBLUE} `get_branch` ${NC}${RED}`parse_git_dirty`${NC} \n${GREEN}$ ${NC}"

i.e. changed \$(get_dir) to get_dir

Result on Cygwin:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2025-02-18 15:12:58

在尝试“修复”它时,您可以在导出ps1 IS 执行时执行get_dir

相反,每次生成新提示时,您都需要对其进行重新评估。也许最简单的方法是定义函数my_prompt,该>在调用时,计算PS1字符串,然后设置

export PS1='$(my_prompt)'

此功能,还可以使您在在提示符中激活函数之前调试函数。

In your attempt to "fix" it, you execute get_dir by the time the export PS1 is executed.

Instead, you need to have it reevaluated every time a new prompt is generated. The perhaps simplest way to do it is to define a function my_prompt which, when called, calculates the PS1 string, and then set

export PS1='$(my_prompt)'

This also allows you to debug your function before you activate it in the prompt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文