When I began teaching Unix around anno 1986, the Unix ls(1) utility printed a list of files to stdout, one per line. It was trivial to explain the concept of not requiring a program which tells you how many files you have in a directory:

you have a program which outputs one file name per line, and you have another program which counts lines of input; if you combine the output of the first with the input of the second via a pipe you get the information you want

This simple idea became a bit more cumbersome to explain when ls changed its default behaviour to output file names in multi-column format: students understandably didn’t immediately realize that ls was playing tricks with isatty(3) when they invoked the program in a shell. Of course not, how should they?

Sure, we then showed the effect with ls -1 at a terminal, and demonstrated with ls | cat, etc., but it just wasn’t the same. (At the time I changed from using ls to who(1) – easy to do because these were multi-user systems, and the examples made sense.)

Why do I mention this?

Today in some versions of Linux ls puts single quotes around file names which contain white space likely in order to have those paths easier to copy and paste, but it does so only if !isatty().

I’m not a great fan of changing a program’s well-known behaviour, specially in a case such as with GNU ls which already uses an environment variable for coloring output; it would likely have been easy to augment that for the file name quoting.

It used to be simpler to teach. (But I don’t really teach Unix beginners any more.) :-)

Updates

  • I learn from Quoting File names, that this behavior was introduced in 2016 already; I’d not noticed, probably because I typically don’t embed spaces in filenames.
  • Quite a few interesting comments on Hacker news.
  • Somebody also submitted this to Lobste.rs.