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?


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.

  • Hmmm... how would I get it to display only if the current directory has a cf project (or alternatively, a git repo?) – Costa Jan 23 '15 at 16:15
  • I'm doing: function cfinfo(){ if [ -d .git ]; then cf target | awk '$1=="Org:"{printf "%s-", $2} $1=="Space:"{print $2}' fi } – Costa Jan 23 '15 at 16:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.