Why `ls` list multiple files per line, but `ls pipe/redirect` list just 1 file per line?
bash, linux, shell
Solution
You can check this line from the source:
if (format == long_format)
format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);
It uses the isatty function to check if `stdout` points to a tty, and to print `many_per_line` if it does or `one_per_line` if it does not.
Problem
Just curious, this is normal-expected behavior of `ls`: ``` user@host:~$ ls Codes Documents Music Pictures Templates Desktop Downloads Papers Public Videos ``` But when I use `ls` with pipe/redirection, it behave like `ls -1`: ``` user@host:~$ ls | cat Codes Desktop Documents Downloads Music Papers Pictures Public Templates Videos ``` Why? (and how to write such program that gives difference output between stdout and pipe like this?) P.S. I also set `alias l='ls -F'`, and this time pipe/redirection is no longer `ls -1` style: ``` user@host:~$ l | cat Codes/ Documents/ Music/ Pictures/ Templates/ Desktop/ Downloads/ Papers/ Public/ Videos/ ``` Without using the alias, it does the command in `ls -1` style, however: ``` $ ls -F | cat Codes/ Desktop/ Documents/ Downloads/ Music/ Papers/ Pictures/ Public/ Templates/ Videos/ ```