zsh comment character
This is due to how Bash/Zsh is configured to handle comment characters in interactive and non-interactive shells.
Normally, comment characters are not recognized in interactive shells by default, so this behavior is normal.
From the Bash Reference Manual, Chapter 3.1.3 - Comments:
In a non-interactive shell, or an interactive shell in which the interactive_comments option to the
shopt builtin
is enabled , a word beginning with ‘#’ causes that word and all remaining characters on that line to be ignored. An interactive shell without theinteractive_comments
option enabled does not allow comments. Theinteractive_comments
option is on by default in interactive shells.
The difference between interactive and non-interactive shells is that in the former, input and output is via the user's terminal whereas in the latter commands are asynchronous (no interaction). Though you are in an interactive shell, when you start a script, a non-interactive subshell is created.
- Interactive Shells (Chapter 6.3)
- Command Execution Environment (Chapter 3.7.3)
Though I've referenced Bash in this answer, Zsh is the same. Chapter 6.7 - Comments reads almost the same:
In non-interactive shells, or in interactive shells with the INTERACTIVE_COMMENTS option set, a word beginning with the third character of the
histchars
parameter (‘#’ by default) causes that word and all the following characters up to a newline to be ignored.
If there is / was a good reason why the feature was disabled, I would like to understand any reasons.
I hesitate to use the word "disabled" as it's merely an environment variable that sets up the execution environment; it's simply "not set" by default. There's no documentation as to the reasoning why Apple (ultimately) chose to set it.
I can confirm that in other operating systems I've tested (FreeBSD and Debian), I installed Zsh (v5.8) and in both, the behavior described is the same as it is here - it's not set. This points to a decision by the Zsh folks that this was their preferred behavior of the execution environment. As we can't speculate as to why Apple did something, it's even more difficult to speculate why Zsh did/didn't do something and why Apple didn't modify it. Though it's probably a safe bet that Apple just went with the defaults.