Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Bash compiled or interpreted?

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?

like image 866
Jon .C Avatar asked Jan 20 '17 02:01

Jon .C


People also ask

Is bash an interpreted language?

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.)

Does bash need to be compiled?

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.

What kind of language is bash?

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.

Does shell script compile?

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.


2 Answers

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.


Related:

  • Shell Operation - GNU Bash Manual
  • Is Perl a compiled or an interpreted programming language?
  • Is bash an interpreted language?
like image 74
codeforester Avatar answered Nov 15 '22 15:11

codeforester


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.

like image 38
paxdiablo Avatar answered Nov 15 '22 15:11

paxdiablo