Date: Sun, 11 Oct 2020 16:26:58 +0700
From: Budi <[email protected]>
Message-ID:
<cah0gyzacpb2dtzkqknwzzllh4rgnrvrsv31_ayxg3hwcdq_...@mail.gmail.com>
| set -n not work as its supposed job to check validity of a command
That is not what it does. When -n is set, commands are not executed,
simply parsed.
| $ set -n 'echo HI' &&echo Y
| Y
What that does is turn on the -n option, and also set $1 to 'echo HI'
which is not what you intended I think. Since the set command succeeds
the "echo Y" then runs.
It isn't really clear anywhere when -n (when set) takes effect (it
is usually only ever used on the command line as in
bash -n script
to have the script parsed, but not executed. That's what the
"check validity" is about - and note that it only checks for
syntax errors, so something like
| $ set -n 'eco HI' &&echo Y
even if it was done properly, couldn't work, as the shell does not
try to execute the 'eco' command when -n is in effect (assuming this
was rewritten so that an attempt would be made) so it never discovers
that there is no such command.
| won't do the check, how to solve ?
Depends on what you're really trying to do, which cannot possibly be
to discover whether "eco" is a known command or not, or at least I
hope not.
kre