5

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?

1
  • 2
    Much of this quoting only happens when the output from ls is output to a terminal. ls | cat shows the actual name (in a format that is unsuitable for re-entering into the command line). Commented Mar 19 at 2:36

1 Answer 1

10

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .