ls -b should print the file names with non-printable characters, for example, ex ex.txt should be shown as ex\ ex.txt, but this does not work on macOS, it is still ex ex.txt. Does anyone know the reason?
Asked
Active
Viewed 336 times
3
Stephen Kitt
- 434,908
sudoer
- 33
1 Answers
3
The -b option isn't standardised, so its behaviour is implementation-dependent.
Many Linux systems use GNU ls by default; it defines the -b option as
print C-style escapes for nongraphic characters
and uses an elaborate quoting implementation. The space character is a nongraphic character so it ends up being escaped.
On macOS the definition of -b is different:
-B Force printing of non-printable characters (as defined by
ctype(3)and current locale settings) in file names as\xxx, where xxx is the numeric value of the character in octal.-b As -B, but use C escape codes whenever possible.
The ctype functions (or macros) consider that the space character is printable, so it's not escaped.
Stephen Kitt
- 434,908
-
A way to mimic the behaviour of Coreutils
ls -bwould likely bels -b | sed 's/ /\\ /g'. I don't have a mac to test this at the moment. This would catch spaces, at least, but I don't know about other characters. – Wyatt Ward Nov 24 '17 at 07:08 -
1That would work in some cases, but post-processing the
lsoutput is touchy in general because you can’t distinguish between file names and the rest ofls’s output. – Stephen Kitt Nov 24 '17 at 10:21
echo $SHELL) – Julie Pelletier Nov 02 '16 at 06:23lscommand does normally treat it that way, at leastls (GNU coreutils) 8.23from bash. – Julie Pelletier Nov 02 '16 at 06:51lsdisplays filenames. Several distributions have already reverted it but if yours hasn't you now need to include the-Nflag. Everywhere. See Why is 'ls' suddenly wrapping items with spaces in single quotes? – Chris Davies Nov 02 '16 at 08:16bash sh csh ksh tcsh zsh, so it should not be caused by the shells. I think @StephenKitt answered the question. – sudoer Nov 02 '16 at 20:07