help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking validity of a command


From: Dennis Williamson
Subject: Re: Checking validity of a command
Date: Sun, 11 Oct 2020 13:52:51 -0500

On Sun, Oct 11, 2020, 10:48 AM Alan D. Salewski <salewski@att.net> wrote:

> On 2020-10-11 10:48:53, Budi spake thus:
> > How to check validity of a command using Bash command inside a script ?
> > e.g. for echo command checking, tried this
> >
> > $ set -n 'echo HI' &&echo Y
> > Y
> >
> > $ set -n 'eco HI' &&echo Y
> > Y
> >
> > won't do the check, how to solve ?
>
> Hi Budi,
>
> It looks like your examples were tried interactively in the shell.
> The bash(1) section for the 'set' builtin notes that '-n' is ignored
> for interactive shells:
> <quote>
>       -n      Read commands but do not execute them.  This may be
>               used to  check a shell script for syntax errors. This
>               is ignored by interactive shells.
> </quote>
>
> Also note that the '-n' option is checking only for syntax
> errors. The 'eco' token is not a syntax error, though bash will
> complain when it later attempts to run it:
>
>     bash: eco: command not found
>
> To have bash syntax-check a script, you can place 'set -n' at the
> top of the script and then run it. But a less intrusive way to do
> the same thing would be to simply invoke the script like this:
>
>     $ bash -n ./some-script
>
> That has the same effect, but does not require modification of the
> script itself.
>
>
If you want to check a single command or a one-liner, as it appears the OP
would like to do, you can invoke a non-interactive shell and check the
syntax like this:

bash -cn 'echo (' && echo Y

>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]