Nicholas Summers
Nicholas Summers

Reputation: 4756

Bash $PS1 Trouble (ubuntu)

So I have a script in my .bashrc for customizing my prompt (see below).

function git_unpushed {
    brinfo=$(git branch -v)
    if [[ $brinfo =~ ("[ahead "([[:digit:]]*)]) ]]
    then
        echo "Not Pushed: ${BASH_REMATCH[2]}"
    fi
}

function git_untracked {
    untracked=$(git clean --dry-run | wc -l)
    if [ $untracked -gt 0 ]
    then
        echo "Untracked: "$untracked
    fi
}

export PS1="\
$(
    # last_result=$?
    uid="$(id -u)"
    host="\[\e[97m\]\H"
    path="\[\e[94m\]\w"
    
    # If root
    if [ "$uid" = "0" ];
    then
        user="\[\e[95m\]\u"
        symbol="\[\e[97m\]#"
    else
        # If not root
        user="\[\e[96m\]\u"
        symbol="\[\e[97m\]\$"
    fi
    
    # If Git Repo
    if [ -d './.git' ];
    then
        unpushed=$(git_unpushed)
        untracked=$(git_untracked)
        branch=$(__git_ps1)
        status=$(git diff --shortstat)
        second_line="hi"
    else
        second_line=$path
    fi
    
    echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
)"

My Question: Why does the path not get replaced whenever I cd to a git repo? (it does if I start a bash prompt within the repo"

I am using Ubuntu 14.04


Update:

After lot of work getting it just right, he is my result: Custom $PS1

Thanks everyone who helped out!

Upvotes: 1

Views: 634

Answers (1)

John B
John B

Reputation: 3646

Edit:

As @EtanReisner points out, your code should work as intended for all users by enclosing your command substitution in single quotes.

    export PS1='\
    $(
        # last_result=$?
        uid="$(id -u)"
        host="\[\e[97m\]\H"
        path="\[\e[94m\]\w"

        # If root
        if [ "$uid" = "0" ];
        then
            user="\[\e[95m\]\u"
            symbol="\[\e[97m\]#"
        else
            # If not root
            user="\[\e[96m\]\u"
            symbol="\[\e[97m\]\$"
        fi

        # If Git Repo
        if [ -d "./.git" ];
        then
            unpushed=$(git_unpushed)
            untracked=$(git_untracked)
            branch=$(__git_ps1)
            status=$(git diff --shortstat)
            second_line="hi"
        else
            second_line=$path
        fi

        echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
    )'

Old Answer:

This is because what you want to happen is only running every time your ~/.bashrc gets sourced. To get it to run after every command you execute, you can create a function and set the environment variable PROMPT_COMMAND to that function.

Try this:

new_ps1 (){
    export PS1="\
    $(
        # last_result=$?
        uid="$(id -u)"
        host="\[\e[97m\]\H"
        path="\[\e[94m\]\w"

        # If root
        if [ "$uid" = "0" ];
        then
            user="\[\e[95m\]\u"
            symbol="\[\e[97m\]#"
        else
            # If not root
            user="\[\e[96m\]\u"
            symbol="\[\e[97m\]\$"
        fi

        # If Git Repo
        if [ -d './.git' ];
        then
            unpushed=$(git_unpushed)
            untracked=$(git_untracked)
            branch=$(__git_ps1)
            status=$(git diff --shortstat)
            second_line="hi"
        else
            second_line=$path
        fi

        echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
    )"
}
PROMPT_COMMAND="new_ps1"

Upvotes: 4

Related Questions