ls output display a file named "N'*" as "N'''*"

System:

  • Ubuntu 22.04.3 LTS
  • GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
  • ls (GNU coreutils) 8.32

Situation:

$ touch "N'*"
$ ls
'N'''*'

"GNU Coreutils – Quoting File names" states:

“Files with single quotes are printed in a ridiculous way!” This issue was quickly fixed in version 8.26:

$ touch "Don't README.txt"

$ ls-8.25 

'Don'''t README.txt'   ## version 8.25


$ ls 

"Don't README.txt"      ## version 8.26 and later
(bug22696#19)

Question:
Is someone able to explain the difference in handling of the single quote between the above example and my file?

Asked By: yossi-matkal

||

I tested this a bit and it looks like ls falls back to the separate single quotes approach when the file name contains a single quote and some other special character other than a space. For example:

$ cat fileNames
one space
one'quote
quote'and*
quote'and?

$ while IFS= read -r file; do touch "$file"; done < fileNames 

$ ls
 fileNames  "one'quote"  'one space'  'quote'''and*'  'quote'''and?'

I’m guessing the devs just figured it’s too complicated to catch all possible failing names when double quoting, so they revert to the—harder to read but easier to write—single quoting for complex cases.

Note that the example does work (on my system, at least):

$ touch "Don't README.txt"
$ ls
"Don't README.txt"

You need another special character to get the other quoting style:

$ touch "Don't README.*" "Don't README.txt"
$ ls
'Don'''t README.*'  "Don't README.txt"
Answered By: terdon
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.