When Bash runs your script, does it parse it as code, or does it parse it as a command? Does Bash actually compile, and run your script like Python would do, or does Bash just run it through its command parser?
Bottom line: Yes, bash is an interpreted language. Or, perhaps more precisely, bash is an interpreter for an interpreted language. (The name "bash" usually refers to the shell/interpreter rather than to the language that it interprets.)
Conclusion. So in short, you don't need a compiler for bash but you need one for C because those languages are converted into actual computer actions differently, and those different way of doing that were chosen because the languages had different goals.
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.
shc itself is not a compiler such as the C compiler, it rather encodes and encrypts a shell script and generates C source code with the added expiration capability. It then uses the system C compiler to compile the source shell script and build a stripped binary which behaves exactly like the original script.
Bash is a single-pass interpreter which means it reads one command at a time, interprets, and runs it then and there. The same thing is true with other types of shells - sh, ksh, zsh, csh, etc.
Here is an example. I have a 3 line script called test.sh
which looks like this:
echo one
echo two
'
When run as bash test.sh
, it gives this output:
one
two
test.sh: line 3: unexpected EOF while looking for matching `''
test.sh: line 4: syntax error: unexpected end of file
It runs the first and second commands successfully and then encounters the dangling single quote and throws the error.
Let's say we write the same code in Perl, test.pl
:
print "one\n"
print "two\n"
'
and run it with perl test.pl
. We get:
syntax error at test.pl line 2, near "print"
Can't find string terminator "'" anywhere before EOF at test.pl line 3.
So, it didn't run the first two lines at all, though they were syntactically correct. That's because Perl makes two passes. In the first pass, it does syntax checks and converts the script into an internal form. In the second pass, it runs it.
The simplicity of shell's single-pass execution is its biggest limitation as well. Tolerating syntax errors, even running at all, makes it hard to build large and robust code with the shell language. However, shell scripting is an ideal choice for quick and throw-away code, especially something that makes use of a lot of command line utilities.
The bash
shell, as it stands now (version 4.4), runs your scripts in a purely textual fashion. It does not do any pre-compilation of files into some form of byte code.
As per the source code, the shell itself just uses reader_loop()
to process the input. This calls, within a loop, read_command()
followed by execute_command()
.
As the read_command()
also contains the call to parse_command()
which calls the YACC parser function yyparse()
, this means that parsing happens on a line-by-line basis, not up front in some large compilation phase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With