From: David Laight <david.laight.linux@gmail.com>
To: Kevin Locke <kevin@kevinlocke.name>
Cc: Jonathan Corbet <corbet@lwn.net>,
Randy Dunlap <rdunlap@infradead.org>,
Thorsten Leemhuis <linux@leemhuis.info>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tools: remove unnecessary x suffix in test strings
Date: Fri, 17 Oct 2025 15:12:56 +0100 [thread overview]
Message-ID: <20251017151256.111f2669@pumpkin> (raw)
In-Reply-To: <a1fb08a30cbd6682e3ca218447573d4c62034003.1760658427.git.kevin@kevinlocke.name>
On Thu, 16 Oct 2025 17:47:09 -0600
Kevin Locke <kevin@kevinlocke.name> wrote:
> An "x" suffix was appended to test variable expansions, presumably to
> avoid issues with empty strings in some old shells, or perhaps with the
> intention of avoiding issues with dashes or other special characters
> that an "x" prefix might have avoided. In either case, POSIX ensures
> that such protections are not necessary, and are unlikely to be
> encountered in shells currently in use, as indicated by shellcheck
> SC2268.
>
> Remove the "x" suffixes which unnecessarily complicate the code.
>
> Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
> Suggested-by: David Laight <david.laight.linux@gmail.com>
> ---
>
> Thanks David, that's a good point about the x suffixes. Since
> shellcheck warns about the x prefixes (SC2268) and I'm not aware of any
> shells currently in use which require them,
The problems arise when $1 is (say) "-x", a simple LR parser will treat
[ -x = -x ] as a check for the file "=" being executable and then give
a syntax error for the second -x.
I can't imagine why shellcheck should warn about a leading x (or any other
character) provided field splitting is disabled (eg by "").
The leading x has definitely been needed in the past.
POSIX does require the three argument 'test' look for the middle argument
being an operator - but there might be historic shells that don't so that.
OTOH you are probably looking for code from the early 1980s!
But the POSIX spec (last time I read it) does point out the problems
with arbitrary strings being treated as operators causing complex expressions
be mis-parsed - which a leading x fixes.
> I think they are safe to
> remove to clean up the code a bit. Here's a patch to do just that,
> which can be applied on top of my previous patch.
>
> Since -o is an XSI extension to POSIX, I've stuck with ||, but I think
> you are right that x would not be required in that case either.
I'm not sure there are any common shells that don't support -o and -a.
They get used quite a lot.
I'm pretty sure they were supported by the pre-POSIX System-V shells
(or the /bin/[ program they ran).
David
>
> Thanks again,
> Kevin
>
>
> tools/debugging/kernel-chktaint | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/debugging/kernel-chktaint b/tools/debugging/kernel-chktaint
> index 051608a63d9f..051ac27b58eb 100755
> --- a/tools/debugging/kernel-chktaint
> +++ b/tools/debugging/kernel-chktaint
> @@ -18,8 +18,8 @@ retrieved from /proc/sys/kernel/tainted on another system.
> EOF
> }
>
> -if [ "$1"x != "x" ]; then
> - if [ "$1"x = "--helpx" ] || [ "$1"x = "-hx" ] ; then
> +if [ "$1" != "" ]; then
> + if [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
> usage
> exit 1
> elif [ $1 -ge 0 ] 2>/dev/null ; then
next prev parent reply other threads:[~2025-10-17 14:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-11 21:04 [PATCH] tools: fix == bashism in kernel-chktaint Kevin Locke
2025-10-11 22:07 ` Randy Dunlap
2025-10-11 22:26 ` Randy Dunlap
2025-10-13 6:52 ` Thorsten Leemhuis
2025-10-13 14:41 ` [PATCH v2] " Kevin Locke
2025-10-13 16:46 ` Randy Dunlap
2025-10-16 20:47 ` [PATCH] " David Laight
2025-10-16 23:47 ` [PATCH] tools: remove unnecessary x suffix in test strings Kevin Locke
2025-10-17 4:00 ` Randy Dunlap
2025-10-17 14:12 ` David Laight [this message]
2025-10-17 22:28 ` Kevin Locke
2025-10-19 10:17 ` David Laight
2025-10-20 20:18 ` Kevin Locke
2025-10-21 8:59 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251017151256.111f2669@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=corbet@lwn.net \
--cc=kevin@kevinlocke.name \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@leemhuis.info \
--cc=rdunlap@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.