4

I'm connected to a bash shell on my ubuntu 16.04 server using Putty on Windows.

This is what it looks like in Putty when I do sudo fdisk -l:

fdisk with bolding

But when I try to pipe the output into less, the colors are not shown:

fdisk piped into less

I have tried sudo fdisk -l | less -R and sudo fdisk -l | less -r, but the output never looks the same as the raw output.

How can I get the output from less to look the same?

Celada
  • 44,132
localhost
  • 243
  • The problem was getting fdisk to output colors when piped more than getting less to retain colored output. – localhost Oct 22 '16 at 06:49
  • @localhost the answer is the same, so it's actually a duplicate – phuclv Oct 22 '16 at 10:02
  • 1
    @LưuVĩnhPhúc nowhere in that question do I see fdisk mentioned. The arguments are the same, but someone reading that question could not know that fdisk happens to have the same color argument as git diff. – localhost Oct 22 '16 at 10:08

1 Answers1

11

The culprit is not less, but fdisk: Many programs only color their output when they print to a terminal, and don't color it when the output is redirected, because in general you want to avoid breaking scripts with the ESC-codes used for colors.

However, usually these programs also have switches to manually turn on coloring. So try

sudo fdisk --color=always -l | less 

instead, and look it up in the man-page for other commands.

dirkt
  • 32,309