Ubuntu – Editing the Bash prompt

bashcommand linegnome-terminalprompt

I'd like to put something into my bash prompt, but I'm not quite sure how to do it.

I use the Cloud Foundry Command Line Tool to push code up to my servers. The cf tool can be set to a specific ORG or another, and within each set to a specific SPACE ('development' or 'production' for example).

Currently my bash prompt looks like this:

 (bug3) $ 

It's actually colored quite a bit more nicely than than : ) 'bug3' is a git branch, and if I had done any work, there'd even be an asterisks 'bug3*' to denote my git status.

If I run cf target -s production I get some output:

API endpoint:   https://api[dot]domain[dot]com (API version: 4.10.1)   
User:           costa[at]domain[dot]com   
Org:            IB   
Space:          Production   

I want to get the bash prompt looking like this:

 (bug3) IB-Production $ 

How might I do that?

Best Answer

  • Edit your ~/.bashrc file and add this at the end:

    function cfinfo(){
      cf target | awk '$1=="Org:"{printf "%s-", $2} $1=="Space:"{print $2}'

    That function generates the string IB-Production depending on the output of the command cf target -s production.

    Then add this after that function definition in ~/.bashrc:

    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w \n $(cfinfo) \$ '

    Notice: The part before $(cfinfo) differs in your case. In my example it is just the default uncolored debian prompt.