I am curious how does iterm2 work? I mean it would be a gargantuan task to re-implement all the unix commands and I guess it somehow uses the built-in terminal under the covers to run the actual commands but how?
Both applications: built-in Apple Terminal and iTerm2 are only terminal emulators. They do not implement any commands, they just take care of the display.
When you open either a Terminal or an iTerm2 window, it automatically runs a shell (like bash or ksh). This is the program that interprets most of the commands that you know as CLI. When you type a command into a shell, it first tries to match it with its internal set of commands and interprets it accordingly. If a command is not found, the shell will try to find a program which name matches the command. This is what you might perceive aa "unix commands", however they are just standalone programs.
For example: If you run a Terminal on a Mac, it runs bash shell by default. Then if you type
date, the shell will find it is not an internal shell command, so it will search the disk (it will search only in places defined in a so called "PATH environment variable") and it will most likely find a program in the following place
/bin/date. The shell will execute this program. The Terminal will then print the standard output of the
date command in its window.
iTerm2 does not use Terminal in the background, but it will run the same shell. So the flow is: iTerm2 runs bash (by default), bash then executes a
date command and
date command will send its output to the iTerm2 which will display the characters for the user.