Skip to Content
Technical Articles

Terminal Tip: truncating CF output

(Find more terminal tips here:

I am a big fan of the terminal, and it’s my preferred work environment for many reasons.

I use the Cloud Foundry CLI cf frequently in my work on the SAP Cloud Platform but the output options are limited, and sometimes hard to read. One example is the output from cf apps or cf services (there are short versions of these two commands, cf a and cf s respectively).

This screenshot shows some typical output from the cf s command:

There’s a lot of information and it wraps onto new lines. Most of the time my focus is on the names of the service instances, and perhaps the service & plan combination they represent – the information towards the end of the line is less important to me. But it’s still being output and making the entire results difficult to read.

With the use of two venerable shell commands, we can fix that.

tput will give us information on the current terminal capabilities. Running tput cols returns the number of columns in the current terminal.

cut will slice and dice data in many ways; I use it to pick out various fields from output lines, but it can also pick out ranges of characters too.

A combination of these two commands, also making use of the command substitution technique with $(...) (this is the newer & better version of using `...`backticks) gives us the ability to truncate the output so that it’s a lot more readable:

Here’s a breakdown of the command:

cf s | cut -c -$(tput cols)

Read it like this:

  1. Run the cf s command
  2. Pipe the STDOUT of that into the STDIN of the cut command
  3. To the cut command execution, supply a value for the -c option which selects specific characters
  4. Which specific characters? Well, the range (x-y) that goes from 1 (implicit) to whatever the command tput cols outputs.
  5. In the case of my terminal shown in the screenshots, tput cols tells me there are 101 columns, so the effective value for the range given to -c is 1-101.

We can make this useful combination into a handy function by defining a shell function, like this:

trunc () { cut -c -$(tput cols); }

Now we can use trunc like this:

cf s | trunc

which gives us the same thing. Lovely!

Share & enjoy, and remember, #TheFutureIsTerminal!

You must be Logged on to comment or reply to a post.
    • Cheers Witalij, yes, it is a little surprising. The general neatness and processability of the output of cf commands is, shall we say, a little “underwhelming”. But we can fix it with the wonderful decades-old tools that we know and love.

      There is JSON output (via cf curl) and while that is a nice alternative approach it doesn’t work for the regular user because the whole command / API structure is different, as well as the output content. It’s often touted as the “answer” to this issue with cf output, but it unfortunately isn’t.