Shell Programming  «Prev  Next»
Lesson 5Interpreted and compiled programs
ObjectiveExplain 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.

Distinction between Compilers and Interpreters

There is a fundamental distinction between compilers and interpreters.
Key Differences:
  1. Program as Data:
    • Interpreters treat programs as data. This means they can evaluate, modify, or execute the program dynamically during runtime.
    • Compilers, on the other hand, transform the source code into an independent machine-executable format (binary) in a single pass or multi-pass process. Once compiled, the resulting executable is no longer treated as data.
  2. Dynamic Manipulation:
    • Because interpreters execute code directly, they can analyze and modify the code while running. For instance, they can:
      • Evaluate expressions dynamically.
      • Support interactive or REPL environments (like Python or Lisp interpreters).
      • Allow for runtime introspection and modification (e.g., metaprogramming).
    • Compilers do not have this flexibility. The compiled program is static and unchangeable without recompilation.
  3. Use Cases:
    • Interpreters are often used for dynamic, high-level languages like Python, Ruby, and JavaScript, where flexibility and runtime manipulation are needed.
    • Compilers are used for languages like C++ or Rust, where performance and static analysis are prioritized.
  4. Performance:
    • Interpreters are generally slower because they translate and execute code line by line or instruction by instruction during runtime.
    • Compilers generate optimized machine code ahead of time, which typically runs faster but lacks the flexibility of on-the-fly manipulation.

Example Scenarios:
  • Interpreter Example: A JavaScript interpreter running in a browser dynamically evaluates and executes code, making it possible to adjust or re-execute code segments during runtime (e.g., hot code replacement or dynamic scripts in web pages).
  • Compiler Example: A C++ compiler generates a static executable. Any modification to the program requires recompilation.
By treating programs as data, interpreters empower dynamic programming paradigms, but they trade off speed and predictability for this flexibility.
  • 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.

The following diagram discusses 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


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.

SEMrush Software 5 SEMrush Banner 5