Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么bash提示符(PS1)附加ghost命令?_Bash_Shell_Command Prompt - Fatal编程技术网

为什么bash提示符(PS1)附加ghost命令?

为什么bash提示符(PS1)附加ghost命令?,bash,shell,command-prompt,Bash,Shell,Command Prompt,我有一些自定义代码,可以将提示调整为如下所示: (git-branch):~/t/r/u/ncated-dir$ source "$HOME/bin/git-prompt.sh" source "$HOME/bin/truncate.sh" PS1="\$(git_prompt)" # prints current branch PS1+="\[$COLOR_WHITE\]:\[$COLOR_BLUE\]\$(truncate)" # directory path PS1+="\[$COLO

我有一些自定义代码,可以将提示调整为如下所示:

(git-branch):~/t/r/u/ncated-dir$
source "$HOME/bin/git-prompt.sh"
source "$HOME/bin/truncate.sh"

PS1="\$(git_prompt)" # prints current branch
PS1+="\[$COLOR_WHITE\]:\[$COLOR_BLUE\]\$(truncate)" # directory path
PS1+="\[$COLOR_WHITE\]\$\[$COLOR_RESET\] " # '#' for root, else '$'
export PS1
有时,当我使用终端中的箭头键滚动浏览以前的命令时,提示符将捕获约6-7个字符并将其附加到提示符中

#! /bin/bash

COLOR_RED="\033[1;31m"
COLOR_YELLOW="\033[1;33m"
COLOR_GREEN="\033[1;32m"
export COLOR_GREY="\033[1;90m"
export COLOR_BLUE="\033[1;34m"
export COLOR_WHITE="\033[0;37m"
export COLOR_RESET="\033[0m"

function git_prompt {
  local git_status
  local color
  git_status="$(git status 2> /dev/null)"

  if [[ $git_status =~ "Your branch is ahead of" ]]; then
    color="$COLOR_YELLOW"
  elif [[ $git_status =~ "Changes not staged for commit" ]]; then
    color="$COLOR_RED"
  elif [[ $git_status =~ "Untracked files" ]]; then
    color="$COLOR_RED"
  else
    color="$COLOR_GREEN"
  fi

  local on_branch="On branch ([^${IFS}]*)"
  local on_commit="HEAD detached at ([^${IFS}]*)"
  local info
  if [[ $git_status =~ $on_branch ]]; then
    local branch=${BASH_REMATCH[1]}
    info="($branch)"
  elif [[ $git_status =~ $on_commit ]]; then
    local commit=${BASH_REMATCH[1]}
    info="($commit)"
  else 
    info="local"
  fi

  echo -e "${color}${info}"
}
我们看到的问题是:

(master):~/p/test$ <scroll up/down through commands here>
因此,很明显,这不是不敬或破坏执行,但它仍然令人讨厌

我的
.bash\u配置文件
如下所示:

(git-branch):~/t/r/u/ncated-dir$
source "$HOME/bin/git-prompt.sh"
source "$HOME/bin/truncate.sh"

PS1="\$(git_prompt)" # prints current branch
PS1+="\[$COLOR_WHITE\]:\[$COLOR_BLUE\]\$(truncate)" # directory path
PS1+="\[$COLOR_WHITE\]\$\[$COLOR_RESET\] " # '#' for root, else '$'
export PS1
我认为,
git prompt.sh
可能是问题所在,因为每当我在
.bash\u profile
中注释掉那一行时,问题就会消失。但我不明白为什么。在我按下enter键之前,它不应该运行(从而改变PS1)。所以我不确定如何在提示符上获得ghost命令

#! /bin/bash

COLOR_RED="\033[1;31m"
COLOR_YELLOW="\033[1;33m"
COLOR_GREEN="\033[1;32m"
export COLOR_GREY="\033[1;90m"
export COLOR_BLUE="\033[1;34m"
export COLOR_WHITE="\033[0;37m"
export COLOR_RESET="\033[0m"

function git_prompt {
  local git_status
  local color
  git_status="$(git status 2> /dev/null)"

  if [[ $git_status =~ "Your branch is ahead of" ]]; then
    color="$COLOR_YELLOW"
  elif [[ $git_status =~ "Changes not staged for commit" ]]; then
    color="$COLOR_RED"
  elif [[ $git_status =~ "Untracked files" ]]; then
    color="$COLOR_RED"
  else
    color="$COLOR_GREEN"
  fi

  local on_branch="On branch ([^${IFS}]*)"
  local on_commit="HEAD detached at ([^${IFS}]*)"
  local info
  if [[ $git_status =~ $on_branch ]]; then
    local branch=${BASH_REMATCH[1]}
    info="($branch)"
  elif [[ $git_status =~ $on_commit ]]; then
    local commit=${BASH_REMATCH[1]}
    info="($commit)"
  else 
    info="local"
  fi

  echo -e "${color}${info}"
}

如果需要,可以查看
truncate.sh
(以及
.bash\u profile
git prompt.sh
)。如果结果是罪魁祸首,我会将代码编辑到问题中。但是,为了简洁和减少噪音,我现在将其略去。

Bash对提示的长度感到困惑

PS1+="\[$COLOR_WHITE\]\$\[$COLOR_RESET\] 
注意上面的颜色是如何被
\[
\]
包围的?这些标记限定了不推进光标的非打印字符

git\u提示符
缺少它们。您需要将其拆分为两个函数才能正常工作,一个用于颜色,另一个用于信息。例如:

PS1="\[\$(git_color)\]\$(git_info)"