Note that there are some explanatory texts on larger screens.

plurals
  1. PObroken bash prompt wrap line
    primarykey
    data
    text
    <p>I'm customizing my bash prompt on OsX to include git branch plus some marks of the branch state. This breaks line wrap.</p> <p>I know that <a href="https://stackoverflow.com/questions/105349/bash-prompt-in-os-x-terminal-broken">I have to add \[ and \] to prevent this issue</a>, but doing so in the functions does display \[ and \] litteraly.</p> <p><strong>What can I do to escape such sequences in those functions?</strong></p> <p>Disclaimer: those are my first attempts in bash scripting.</p> <pre><code>function parse_git_dirty { # TODO make git status response a variable # [branch+] : working dir has staged changes if [[ $(git status 2&gt; /dev/null | grep "to be committed") ]] then S=$S"$(tput setaf 2)+$(tput sgr0)" fi # [branch+] : working dir has unstaged changes if [[ $(git status 2&gt; /dev/null | grep "not staged for commit") ]] then S=$S"$(tput setaf 1)+$(tput sgr0)" fi # [branch+] : working dir has untracked files if [[ $(git status 2&gt; /dev/null | grep "tracked files") ]] then S=$S"$(tput setaf 1)+$(tput sgr0)" fi # [branch&lt;] : local branch is behind origin if [[ $(git status 2&gt; /dev/null | grep "Your branch is behind") ]] then S=$S"$(tput setaf 5)&lt;$(tput sgr0)" fi # [branch&gt;] : local branch is ahead origin if [[ $(git status 2&gt; /dev/null | grep "branch is ahead of") ]] then S=$S"$(tput setaf 5)&gt;$(tput sgr0)" fi # [branch&lt;&gt;] : branches have diverged if [[ $(git status 2&gt; /dev/null | grep "have diverged") ]] then S=$S"$(tput setaf 5)&lt;&gt;$(tput sgr0)" fi echo $S } function parse_git_branch { git branch --no-color 2&gt; /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' } function show_git_branch { if [[ $(parse_git_branch) ]] then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)" fi } export PS1="\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[\$(show_git_branch)\] " </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload