You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a snippet or screenshot that shows the problem:
#!/bin/bash#Basic example, one line per file:
ls -l ./*.txt | wc -l
#Using -1 is better (no funny date format changes etc):
ls -1 ./*.txt | wc -l
#But even -1 would still map odd characters to question mark#so use -b which is available on macOS and Linux
ls -1 -b ./*.txt | wc -l
Here's what shellcheck currently says:
All three examples trigger SC2012 (info): Use find instead of ls to better handle non-alphanumeric filenames.
Here's what I wanted or expected to see:
Reading https://www.shellcheck.net/wiki/SC2012 highlights the risks in unexpected variation in the ls metadata output (e.g. date/time), which are avoided by using -1 although that does spoil the example searching by username.
Moreover it raises the issue that ls will map some characters to a question mark, potentially giving name collisions, and more importantly resulting in strings which are not valid filenames.
I wonder if you consider -b to be a sufficient safeguard for this?
There are other relevant flags, but this looks to be the most cross-platform (despite not being in the POSIX standard).
On macOS, there are no quoting options as on Linux. However with macOS 12.7 we have:
$ man ls
...
-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. This option is not defined
in IEEE Std 1003.1-2008 (“POSIX.1”).
...
-b As -B, but use C escape codes whenever possible. This option is not
defined in IEEE Std 1003.1-2008 (“POSIX.1”).
...
GNU ls has a different meaning for -B, but does offer other relevant options:
$ ls --version | head -n 1
ls (GNU coreutils) 8.30
$ man ls
...
-b, --escape
print C-style escapes for nongraphic characters
...
-q, --hide-control-chars
print ? instead of nongraphic characters
--show-control-chars
show nongraphic characters as-is (the default, unless program is 'ls' and
output is a terminal)
-Q, --quote-name
enclose entry names in double quotes
--quoting-style=WORD
use quoting style WORD for entry names: literal, locale, shell,
shell-always, shell-escape, shell-escape-always, c, escape (overrides
QUOTING_STYLE environment variable)
...
The text was updated successfully, but these errors were encountered:
For bugs
shellcheck --version
or "online"): onlineHere's a snippet or screenshot that shows the problem:
Here's what shellcheck currently says:
All three examples trigger SC2012 (info): Use find instead of ls to better handle non-alphanumeric filenames.
Here's what I wanted or expected to see:
Reading https://www.shellcheck.net/wiki/SC2012 highlights the risks in unexpected variation in the ls metadata output (e.g. date/time), which are avoided by using
-1
although that does spoil the example searching by username.Moreover it raises the issue that
ls
will map some characters to a question mark, potentially giving name collisions, and more importantly resulting in strings which are not valid filenames.I wonder if you consider
-b
to be a sufficient safeguard for this?There are other relevant flags, but this looks to be the most cross-platform (despite not being in the POSIX standard).
On macOS, there are no quoting options as on Linux. However with macOS 12.7 we have:
GNU ls has a different meaning for -B, but does offer other relevant options:
The text was updated successfully, but these errors were encountered: