Shell Programming  «Prev  Next»
Lesson 5Interpreted and compiled programs
Objective Explain the difference between interpreted and compiled programs.

Interpreted and compiled Programs

Explain the difference between interpreted and compiled programs. When you write a shell script, you enter commands like if and while to instruct the UNIX system what to do. You will be learning more about these commands later in the course. Before UNIX can use these words, they must be converted into instructions that a computer can understand bits and bytes to be specific, or ones and zeros.

Compiled programs

Some programming languages perform this conversion step at the time the program is written. This conversion process is called compiling[1] the program. Compiling is performed by a compiler program that reads a text file written in the programming language and converts it into an executable file that can be read efficiently by the operating system. Languages like C, C++, and assembly language are compiled languages.

Interpreted languages

Other programming languages are converted as the program is being executed. One instruction at a time is read from the text file containing a script by the interpreter, converted to machine-readable form, and then executed. The machine-readable form is never stored on disk; it is reconverted each time the program is run. Languages like Perl, Tcl, Python, and all UNIX shell scripting languages are interpreted languages.
Programs written in interpreted languages are generally called scripts. Hence you hear about Perl scripts, Python scripts, and shell scripts. A script is just an interpreted program.
Move your mouse over the following diagram to learn how both kinds of programming languages work.

Linux Shell Scripting
Compilation versus Interpretation
  1. Developers write a program in a language like C or C++
  2. Each new program feature is tested, requiring many compiles
  3. When completed, the compiled binary program is distributed to users
  4. Users run the program as a binary executable
  5. Developers write a program (script) in a language like the bash shell scripting language
  6. After debugging and testing, the script is distributed to those who will use it
  7. Users run the script on their UNIX systems
  8. The operating system converts each line of the script before executing it

Running Compiled Programs

Advantages of compiled programs versus interpreted:

Key advantages of compiled languages:
  1. Machine-readable files run much faster than a script because the work of conversion is already done.
  2. Data in machine-readable files cannot be deciphered by others. This holds true for C programs that are compiled and generate an executable file that consists of binary code. This binary code protects trade secrets and intellectual property, which prevents modifications to commercial products.
    This page discusses how you can protect your trade secrets by using binary executables.

Key advantages of interpreted languages (scripts):
  1. Because a script does not need to be explicitly compiled before each test run, it is faster to write.
  2. Scripts are more likely to run on a variety of systems because they are distributed as text files and do not rely on special binary formats like a compiled program does.
  3. Scripts can be read and modified by others to meet their specific needs.
Other interpreted languages that can be used for writing scripts are outlined in the next lesson.

[1]Compiling: Converting a human-readable source code file for a program into a computer-readable file that can be executed.