Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Should ls -1 -b be acceptable for SC2012 #2950

Open
2 tasks done
peterjc opened this issue Mar 20, 2024 · 0 comments
Open
2 tasks done

Question: Should ls -1 -b be acceptable for SC2012 #2950

peterjc opened this issue Mar 20, 2024 · 0 comments

Comments

@peterjc
Copy link

peterjc commented Mar 20, 2024

For bugs

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)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant