A binary is another term for a process. So, on POSIX systems, the term is used as a directory name. This naming convention lets others know that this is where to find the executable binary code.
Not all binaries are actually binary. Some of them are scripts. Written in Python, Bash, or JavaScript. They are not yet compiled into the raw binary instructions fed into the CPU. These programs rely on an 'interpreter'. The interpreter makes the conversion at runtime. As the process runs the script is parsed and converted into binary code.
To identify which interpreter is needed by the script, a special character sequence is used called the shebang. The shebang is always the first line in the file, and it always starts with (#!).
Some sample Shebangs
#!/bin/bash
#!/usr/local/bin/python
#!/Users/lee/.nvm/versions/node/v16.13.1/bin/node
To start the process, the shebang tells the kernel to use the interpreter binary and pass the file with the shebang as a parameter.
To write binary code, use a native language like C, C++, Objective-C (but not C#).
Most systems have gcc or cc installed in their path, so it's easy to write hello world in C.
gcc hello-world.c
hello-world.c
#include <stdio.h>
int main() {
printf("Hello World"); // it's a cultural reference
return 0;
}
Bytecode is not a script, and it's not a binary. It's somewhere inbetween. Bytecode has a compiler and an interpreter, bringing the best of both worlds. For C#, the DotNet framework interprets the bytecodes. For Java, it's the JVM. So there is still a compile step, like with native code, however the compiler does not compile into binary it compiles into bytecode
For example, to compile a java program into bytecode use javac
.
javac hello-world.java
hello-world.java
// OOPS
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This produces a file containing the bytecode. To execute the bytecode output use java
java hello-world
Programs that we use every day come in all these different forms. Windows users may know them as .exe files. These are the 3 common types of executables, or binaries. Ultimately, the binary's purpose is to provide instructions to the CPU.