bug-bash
[Top][All Lists]
Advanced

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

Re: Interesting bug


From: Ilkka Virta
Subject: Re: Interesting bug
Date: Sat, 12 Feb 2022 23:11:29 +0200

On Sat, Feb 12, 2022 at 8:25 PM David Hobach <tripleh@hackingthe.net> wrote:

> I guess it closes the function and {echo is interpreted as string or so,
> but that is probably not all (testCode is e.g. never executed).
>

Let's see if I get this right.

Removing the irrelevant parts, your code is pretty much this:

func() {
        {echo hello;}
        { echo funny stuff
          exit 1
}
}

The key here is that '{' is a keyword, like 'if', or 'do', not an operator
like '('. Which just might be
quite different in other programming languages, but here we are. So,
having '{echo' instead of
'{' is a bit like having 'fiecho' instead of 'fi'. Not relevant for the
syntax, so that the first '}' just after
then ends the function and the block with "echo funny stuff" is on the top
level.

You could have this instead, to the same effect:

func() {
    {echo hello
}
if true; then
        echo funny stuff
        exit 1
fi
fi

Without the explicit "exit", you'd get the syntax error after the "echo
funny stuff" was run. The function itself
is never called, so "echo hello", or whatever there is, never runs.

On the other hand, the shell parses lines in full before doing anything
(and it needs to look if there's a redirection
after the block), so with '} }' or 'fi fi' on one line instead, the syntax
error is caught before the block runs.

For fun, try that with 'fi fi', 'fi; fi', 'fi; xyz' and 'fi xyz' and see
which versions run the block (and try to figure out why)


reply via email to

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