I'm using the
sed command and I want to keep colored output from the previous command. The output of
ls is colored, but the output of sed is not. I'm using OSX.
ls -la | sed -En '/Desktop/q;p'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
On macOS, the
ls is not GNU
ls and does not accept the
--color=always option that Linux users might expect for this functionality.
In the macOS version of
ls, the colors are controlled by two variables:
$CLICOLOR_FORCE. If the former is defined, the terminal specified by
$TERM supports color, and the output is to a terminal, then this output will be colored, much like GNU's
--color=auto option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's
So to have color passed through to
sed, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
I'll blindly guess that your distribution, like many, has an alias that maps
ls -la --color=always | sed … should work.
auto setting makes, broadly speaking,
ls check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)