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

Re: is there such a thing as "bytecode" for bash scripts? à la java "bytecode"? ...



On Sat, Sep 06, 2025 at 19:27:06 +0200, lbrtchx@tutamail.com wrote:
> 
> The way I see thingsbash or any other OS script type must have a compilation and decodingphase before it talks to the OS. How do you get that stream of datathat the OS digests?

No, bash does not use an internal bytecode.  That's part of the reason
why bash is so VERY slow compared to other scripting languages.

Most of the other scripting language (Python, Perl, Tcl, etc.) do have
an internal bytecode compilation, at least in some situations.  Python's
is the most visible, because it will attempt to save the bytecode to
a file on disk.  Perl's bytecode is not saved, but perl goes through
a full source file compilation pass before it runs anything.  Tcl's
bytecode is not saved either, but it doesn't do a full byte compilation
pass; it only byte-compiles procedures.  Therefore, running a tight
loop in the "main" body of a Tcl script could be much, much slower than
running the same loop in a procedure.

Bash doesn't compile to bytecode.  It does, however, generate an
Abstract Syntax Tree and reuse it in some situations.  Mostly this
applies to loops, I believe.  Something like this:

    while true; do
        foo=$(something)
        if [[ $foo =~ myregex ]]; then
            break
        fi
    done

In this code example, the internal body of the loop is all parsed at once,
and I *think* the AST is saved, so it doesn't have to re-parse the body
each time through the loop.

If you want a better grasp of bash internals, you should ask on the bash
mailing lists.  I'm more of an end user.


Reply to: