Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

can you please explain how this works?


it's quite a common pattern in commandline apps to check if the stdout is a tty or not, and do things differently if so. So when you pipe it through `cat', it does the no-fancy-included output, much like the colouring that `ls' will apply interactively, but not when piped/redirected.


It's important for grepping and sedding - the colors are escape codes and would mess with your regular expressions otherwise (you want to find foobar , but bar was blue so you got foo\e[1;34mbar\e[0m instead, and it doesn't match with foobar).


On the other hand, if you want colors displayed you can force grep/ack to display the color 'always' and then pipe the output into 'less -R'. The -R (or --RAW-CONTROL-CHARS) flag has less display output such that color escape sequences work as you might expect, coloring the output displayed by less.


ls | cat is also useful because it forces single-column output.


Isn't that the same as ls -1?


Mostly, yes, except that ls -1 does not turn off colours, and maybe there are a few more differences.


Color output is not on by default in 'ls'.

Your 'ls' must be an alias for the real ls with some options. For example, mine is:

    $ type ls
    ls is aliased to `ls --color=auto'
A quick way to run the original 'ls' (not the aliased version), just prefix it with backslash:

    $ \ls ...
Or:

    $ command ls ...


I can't imagine why anyone would want to spawn another process instead of just RTFM, if you are on linux:

    ls --color=never -1


Because, in many - if not most - situations, spawning another process is insanely lightweight compared to the cognitive/physical load of your alternative (which includes remembering the switch and its value ("never" is hardly intuitive), remembering whether or not you're using a system that supports it (surprise, surprise, OSX doesn't), and typing all those extra characters).

Admittedly, I never actually do this because I don't ever care to not see colour (what would be the motive?), but I can understand why the OP does what they do.


You are Randal L. Schwartz and I claim my five pounds.

[http://partmaps.org/era/unix/award.html]




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: