In the last post, The Terminal and the Shell, I mentioned that opening another terminal tab normally starts another instance of your shell program, but in a separate process. Let's quickly differentiate programs, instances, and processes.
Programs
Let's use bash as an example. bash is just an executable file containing the instructions that make it work. Also known as a program. You can install programs and then never use them; their files just sit there and take up space. bash is not one of those, it will see plenty of use.
> which bash
/bin/bash
> file /bin/bash
/bin/bash: Mach-O universal binary with 3 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e] [arm64e.x1]
/bin/bash (for architecture x86_64): Mach-O 64-bit executable x86_64
/bin/bash (for architecture arm64e): Mach-O 64-bit executable arm64e
/bin/bash (for architecture arm64e.x1): Mach-O 64-bit executable arm64e.x1
> file /opt/homebrew/bin/node
/opt/homebrew/bin/node: Mach-O 64-bit executable arm64
Above explained:
- I locate where
bashis installed - I use
fileto get more information about it. It reveals thatbashis a universal macOS binary. This means that multiple versions ofbashare compiled for each architecture, and they are bundled into one file. At runtime, the system determines which version to use based on the architecture of the running machine. - For comparison, I show that
/opt/homebrew/bin/nodeis a regular binary on macOS.
Instances
When you run any program on your computer, your shell makes a system call behind the scenes to the kernel. The kernel then creates a new process ID (PID) and then executes the program, creating a new instance of it. An instance is just one particular execution of the same program. This is analagous to having a class in object-oriented programming and creating multiple instances of it.
Processes
A process executes an instance of a program with its own memory address space and execution state, meaning every process executes independently of one another, even if they are running the same program.
So, again, if you run the same bash program in two separate tabs in your terminal, you'll have two separate bash processes running independently of each other.