Tuesday, September 26, 2017

In learning Powershell, one of the hardest things to wrap my head around is how it is, and is not, like Unix shells. While it does allow you to interact with your system and perform actions, such as running commands, executing scripts, or doing the same to remote systems, there are some differences. Like... everything is an object, instead of everything is a blob of text. This has taken some getting used to, but in many ways it really simplifies a lot of activities that in a Unix shell would take a pretty complex pipeline.

People often conflate the Unix shell, such as Bash, or Zsh, and the terminal, such as xterm, Gnome Terminal, iTerm, etc. The terminal is the application or hardware through which a user can interact with the Unix system. Back in the last millenium, the terminal started off as a teletypewriter, from which modern Unix and Unix-like operating systems still retain the 'tty' name. Nowadays, it's also common to see purely software implementations referred to as pseudo-terminals (pty), because they serve the same function but have no physical manifestation, which are used by Terminal Emulators. The shell that runs in the terminal is how users truly interact with the system. Bash, Zsh, Csh, Ksh, and their many derivatives or specialized shells (such as may be used at a car dealer, or a POS system, etc), are the utility through which the terminal becomes useful.

The only difference between a bash shell on the system console, tty0, and a bash shell running in a terminal emulator on a remote system connected via ssh, is the capabilities of the terminal to display output, and control input. The bash shell itself will be the same, and generally speaking, everything in the Unix shell is text. We run a command, such as `ls`, and we get back a blob of text that we, as humans, can interpret as a list of files and directories. We can further process, or parse, that text using tools like awk, sed, grep, etc. These additional tools allow us to filter output, take an action on a string in the output, count the number of items, etc, etc.

In the PowerShell ecosystem, the analog of the Terminal is the Host. Microsoft includes the "Console Host", which is the terminal-like window that opens if you run "Microsoft Powershell", and the "Windows Powershell ISE Host", which opens when you run the "Windows PowerShell ISE" application. Both of these windows serve as hosts for the PowerShell shell itself, and they actually provide different behaviors, too as far as output, debugging, etc. In PowerShell, everything is an object. When you run the date alias in PowerShell, you are presented a string representation of the current date and time, but that's just the display. What you actually get back is a [System.Datetime] or (shortened) [datetime]
object, which has all sorts of methods, properties, and metadata associated with it.