I wrote a bash script and executed it compiling. It works perfectly. It can work with or without permissions, but when it comes to C programs, we need to compile the source code. Why?
put on hold as off-topic by countermode, Dmitry Grigoryev, MatthewRock, Rui F Ribeiro, don_crissti 14 hours ago
|
|||||||||||||||||||||
|
It means that shell scripts aren't compiled, they're interpreted: the shell interprets scripts one command at a time, and figures out every time how to execute each command. That makes sense for shell scripts since they spend most of their time running other programs anyway. C programs on the other hand are usually compiled: before they can be run, a compiler converts them to machine code in their entirety, once and for all. There have been C interpreters in the past (such as HiSoft's C interpreter on the Atari ST) but they were very unusual. Nowadays C compilers are very fast; TCC is so fast you can use it to create "C scripts", with a Some languages commonly have both an interpreter and a compiler: BASIC is one example that springs to mind. You can also find so-called shell script compilers but the ones I've seen are just obfuscating wrappers: they still use a shell to actually interpret the script. As mtraceur points out though a proper shell script compiler would certainly be possible, just not very interesting. Another way of thinking about this is to consider that a shell's script interpreting capability is an extension of its command-line handling capability, which naturally leads to an interpreted approach. C on the other hand was designed to produce stand-alone binaries; this leads to a compiled approach. Languages which are usually compiled do tend to sprout interpreters too, or at least command-line-parsers (known as REPLs, read-eval-print loops; a shell is itself a REPL). |
|||||||||||||||||||||
|
Programming/scripting languages can be compiled or interpreted. Compiled executables are always faster and many errors can be detected before execution. Interpreted languages are typically simpler to write and to adapt being less strict than compiled languages, and don't require compiling which makes them easier to distribute. |
|||||||||||||||||||||
|
Consider the following program:
In the Alternatively, you can hand your shopping list to a shopping compiler. The compiler thinks for a while and gives you a new list. This list is LONG, but consists of much simpler instructions:
As you can see, the compiler knows exactly where everything is in the shop so the whole "looking for things" phase is not needed. This is a program in its own right and does not need "Experienced shopper" to execute. All it needs is a human with "Basic human operating system." Returning to computer programs: Both interpreters and compilers have their advantages and disadvantages. |
|||||
|
It all comes down to the technical difference between how the program you can read/write as a human gets converted into the machine instructions your computer understands - and the different advantages and disadvantages of each method is the reason why some languages are written to need compilers, and some are written to to be interpreted. First, the technical difference(Note: I'm simplifying a lot here for the sake of addressing the question. For a more in-depth understanding, technical notes at the bottom of my answer elaborate/refine some of the simplifications here, and the comments on this answer have some useful clarifications and discussion as well..) There are basically two general categories of programming languages:
C is in the first category (the C compiler translates the C language into your computer's machine code: the machine code is saved into a file, and then when you run that machine code, it does what you want). bash is in the second category (the bash interpreter reads the bash language and the bash interpreter does what you want: so there's no "compiler module" per se, the interpreter does the interpreting and the executing, whereas a compiler does reading and translating). You might have already noticed what this means: With C, you do the "interpret" step once, then whenever you need to run the program, you just tell your computer to execute the machine code - your computer can just run it directly without having to do any extra "thinking". With bash, you have to do the "interpret" step every time you run the program - your computer is running the bash interpreter, and the bash interpreter does extra "thinking" to figure out what it needs to do for each command, every time. So C programs take more CPU, memory, and time to prepare (the compiling step) but less time and work to run. bash programs take less CPU, memory, and time to prepare, but more time and work to run. You probably don't notice these differences most of the time because computers are very fast nowadays, but it does make a difference, and that difference adds up when you need to run big or complicated programs, or a lot of little programs. Also, because C programs are converted into machine code (the "native language") of the computer, you can't take a program and copy it onto another computer with a different machine code (for example, Intel 64-bit onto Intel 32-bit, or from Intel to ARM or MIPS or whatever). You have to spend the time to compile it for that other machine language again. But a bash program can just be moved over to another computer that has the bash interpreter installed, and it'll run just fine. Now the why part of your questionThe makers of C were writing an operating system and other programs on hardware from several decades ago, that was rather limited by modern standards. For various reasons, converting the programs into the computer's machine code was the best way towards that goal for them at the time. Plus, they were doing the kind of work where it was important that the code they wrote ran efficiently. And the makers of the Bourne shell and bash wanted the opposite: They wanted to write programs/commands that could be executed immediately - on the command-line, in a terminal, you want to just write one line, one command, and have it execute. And they wanted scripts that you wrote to work anywhere where you had the shell interpreter/program installed. ConclusionSo 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. Other technical/advanced details/notes
|
|||||||||||||||||||||
|
Imagine that English is not your native language (that might be quite easy for you if English is not your native language). There are 3 ways you might read this:
Computers have a "native language" of sorts - a combination of instructions that the processor understands, and instructions that the operating system (e.g. Windows, Linux, OSX etc) understand. This language is not readable by humans. Scripting languages, such as Bash, usually fall into categories 1 and 2. They take a line at a time, translate that line and run it, then move on to the next line. On Mac and Linux, quite a few different interpreters are installed by default for different languages, such as Bash, Python and Perl. On Windows, you have to install these yourself. Many scripting languages do a little pre-processing - try to speed up the execution by compiling chunks of code that will be run often or that would otherwise slow down the application. Some terms you might hear about include Ahead-of-time (AOT) or Just-in-time (JIT) compilation. Lastly, compiled languages - like C - translate the whole program before you can run them. This has the advantage that the translation can be done on a different machine to the execution, so when you give the program to the user, while there may still be bugs, several types of errors can already be cleaned up. Just like if you gave this to your translator, and I mention how the There is one downside to compiled languages however: I mentioned that computers have a native language, composed of features from the hardware and the operating system - well, if you compile your program on Windows, you won't expect the compiled program to run on a Mac. Some languages get around this by compiling to a kind of half-way language - a bit like Pidgin English - that way, you get the benefits of a compiled language, as well as a small speed increase, but it does mean you need to bundle an interpreter with your code (or use one that is already installed). Lastly, your IDE was probably compiling your files for you, and could tell you about errors before you ran the code. Sometimes, this error checking can be more in-depth than the compiler will do. A compiler will often only check as much as it needs to so that it can produce sensible native code. An IDE will often run a few extra checks and can tell you, for example, if you've defined a variable twice, or if you've imported something that you haven't used. |
|||||||||||||||||||||
|