Trying to count number of files in current directory, I found ls -1 | wc -l, which means: send the list of files (where every filename is printed in a new line) to the input of wc, where -l will count the number of lines on input. This makes sense.I decided to try simply ls | wc -l and was very surprised about it also gives me a correct number of files. I wonder why this happens, because ls command with no options prints the filenames on a single line.
Why does ls | wc -l show the correct number of files in current directory?
ls;wc
From info ls:'-1'  '--format=single-column'List one file per line.  This is the default for 'ls' when standard         output is not a terminal.When you pipe the output of ls, you get one filename per line.ls only outputs the files in columns when the output is destined for human eyes.Here's where ls decides what to do:  switch (ls_mode)    {    case LS_MULTI_COL:      /* This is for the 'dir' program.  */      format = many_per_line;      set_quoting_style (NULL, escape_quoting_style);      break;    case LS_LONG_FORMAT:      /* This is for the 'vdir' program.  */      format = long_format;      set_quoting_style (NULL, escape_quoting_style);      break;    case LS_LS:      /* This is for the 'ls' program.  */      if (isatty (STDOUT_FILENO))        {          format = many_per_line;          /* See description of qmark_funny_chars, above.  */          qmark_funny_chars = true;        }      else        {          format = one_per_line;          qmark_funny_chars = false;        }      break;    default:      abort ();    }source: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c