Shellcheck: SC2000 needs explanation/examples

Created on 5 May 2020  Â·  9Comments  Â·  Source: koalaman/shellcheck

https://github.com/koalaman/shellcheck/wiki/SC2000

It appears that if you run "echo $variable | wc -c" or "echo $variable | wc -d", then SC2000 is triggered.

The wiki for SC2000 consists only of a code snippet of it being triggered with "wc -c".

Here's a snippet or screenshot that shows the problem:

echo "$x" | wc -c
echo "$x" | wc -m

Here's what shellcheck currently says:

Shellcheck says "See if you can use ${#variable} instead"
Wiki consists solely of example code using "wc -c" that triggers the check

Here's what I wanted or expected to see:

Wiki to show replacement code that doesn't trigger the issue and an explanation of situations where it wouldn't apply (like "wc -c" example can't be replaced with ${#variable} if the variables could have multi-byte characters).

Most helpful comment

I think it would be useful to add at least the following notes:

  • you should use echo -n to suppress the newline character and count the exact number of bytes in the variable
  • if you only need the number of characters (instead of bytes), you should use ${#variable}

Since the check is triggered on POSIX sh, recommending echo -n would need a caveat.

All 9 comments

I think it would be useful to add at least the following notes:

  • you should use echo -n to suppress the newline character and count the exact number of bytes in the variable
  • if you only need the number of characters (instead of bytes), you should use ${#variable}

I think it would be useful to add at least the following notes:

  • you should use echo -n to suppress the newline character and count the exact number of bytes in the variable
  • if you only need the number of characters (instead of bytes), you should use ${#variable}

Since the check is triggered on POSIX sh, recommending echo -n would need a caveat.

Just a friendly reminder. The wiki can be edited without opening issues.

My bad. I was thinking it was only editable by project members. I've added some comments/code to the wiki.

Ok, but it is not correct to say that "${#variable}" returns the same result as "$(echo "$variable" | wc -m)".

@cslycord, are you sure that echo -n is not standard? I cannot find any reference documentation and on my system dash, busybox sh, sh, /bin/echo all support it.

POSIX defines echo as having no options.

The shells you've tested it on are simply less strict about posix
compliance. For instance, it's less likely to be supported on sh from BSD
and derivatives (such as OS X). On many systems "echo -n" prints "-n" to
the screen.

shellcheck gives a warning about it, if you've set the shebang to /bin/sh

On Wed, May 6, 2020, 9:45 PM Marcello Nuccio ARPAE notifications@github.com
wrote:

Ok, but it is not correct to say that "${#variable}" returns the same
result as "$(echo "$variable" | wc -m)".

@cslycord https://github.com/cslycord, are you sure that echo -n is not
standard? I cannot find any reference documentation and on my system dash,
busybox sh, sh, /bin/echo all support it.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/koalaman/shellcheck/issues/1937#issuecomment-624627267,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AC5TL7L76ESS3M5FGVGNUJLRQFLW3ANCNFSM4MZQVMCQ
.

The standard way to achieve echo -n "$variable" is to use printf "%s" "$variable"

But there's an issue that the ${#variable} warning isn't displayed when you use either of those.
If set to bash:
echo "$1" | wc -c -> SC2000 warning
echo -n "$1" | wc -c -> No errors/warnings
printf "%s" "$1" | wc -c -> No errors/warnings

Should we close this issue and continue on issue #1938?

Fine by me.

Closing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mechalynx picture mechalynx  Â·  5Comments

sobolevn picture sobolevn  Â·  4Comments

arth1 picture arth1  Â·  4Comments

hugovk picture hugovk  Â·  4Comments

erichelgeson picture erichelgeson  Â·  5Comments