koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.29k stars 1.77k forks source link

New check: long options in POSIX sh are not defined #2692

Open urxvtcd opened 1 year ago

urxvtcd commented 1 year ago

For new checks and feature suggestions

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

#!/usr/bin/env sh

echo some | wc --lines

Here's what shellcheck currently says:

Nothing

Here's what I wanted or expected to see:

In POSIX sh, long options are not defined.

urxvtcd commented 1 year ago

I know like basics of Haskell, so I might want to help here. This would probably require creating a list of commands specified by POSIX and checking if they are run with long options. Didn't look into codebase, but that just might be doable. Any thoughts before I try?

brother commented 1 year ago

Aren't you mixing concepts here? getopts will not work with long options. wc on the other hand might be a command that can recognize long options (my wc does for instance).

urxvtcd commented 1 year ago

I don't think I am. wc as specified by POSIX does not accept long options1, and the fact that it does on someone's system and not on some other one can introduce portability issues without them knowing, which is what I'd like shellcheck to report here.

brother commented 1 year ago

but wc is not a builtin, it's still external. there are some commands supported by shellcheck and so on.

urxvtcd commented 1 year ago

I'm afraid I don't understand. Why does it matter whether wc is a builtin or not? I guess it's a useful check to have if one wants to check if their script is portable, and we can't count on wc supporting long options. Am I missing something?

austin987 commented 1 year ago

I suspect @brother's comment was related to the fact that wc is an external program (i.e., not a shell builtin).

That said, (not to speak for them) they may not realize that wc is also part of POSIX, and in POSIX, wc DOES NOT require long options: https://pubs.opengroup.org/onlinepubs/000095399/utilities/wc.html

So, yeah, valid bug.

brother commented 1 year ago

@austin987 exactly that and shellcheck does have some "known" commands where some behaviour is known and supported. That said, shellcheck is not a posix support tool - not even with sh as shebang. I have no opinion regarding adding wc to a list of commands not allowing long options when in sh mode.