It might be useful to allow comments in zsh commands written on the command line, as in bash, but

% echo test # test
zsh: bad pattern: #

Any way to get the same behaviour as in the bash shell?

  • You can add # comments at the end of line and they're written in history (at least in Bash). What do you mean by multiline?
    – user147505
    Dec 16, 2019 at 12:36
  • @Tomasz Zsh history file format has support for multiline commands as a single history item. As in, you press <up>, and a set of lines may be returned to the prompt. It's useful for loops and pipes written across multiple lines. Toothrot probably wants comments in lines prior to code.
    – JoL
    Dec 16, 2019 at 23:04

1 Answer 1

$ setopt interactive_comments
$ echo hello # comment

By default, the zsh shell enables the interactive_comments shell option in scripts (non-interactive shells in general), but not when running an interactive session.

The relevant bit from the zsh manual:

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.

Without this shell option set, you'd only get the bad pattern error if the extended_glob shell option is set. With extended_glob set, x# would match zero or more of the pattern x and x## would match one or more of the pattern x (these correspond to the regular expression modifiers * and +). This means that with extended_glob set and interactive_comments unset, the shell is complaining about the syntax used in an extended filename globbing pattern modifier you used unknowingly.

The values in histchars are by default !^#, and the first two characters are used in history expansions.

Since comments in zsh are delimited by $histchars[3], changing this character will change what text is considered to be a comment:

$ setopt extended_glob
$ echo hello # hello : hello
zsh: bad pattern: #
$ unsetopt extended_glob
$ echo hello # hello : hello
hello # hello : hello
$ setopt interactive_comments
$ echo hello # hello : hello
$ histchars[3]=:
$ echo hello # hello : hello
hello # hello

Interestingly (?), the bash shell also has an interactive_comments shell option, but this is turned on by default in interactive shells:

$ echo hello # hello
$ shopt -u interactive_comments
$ echo hello # hello
hello # hello
  • thanks! any idea why this is not on by default?
    – Toothrot
    Dec 16, 2019 at 12:44
  • I suspect it’s not on by default because it would break backwards compatibility. Dec 16, 2019 at 12:49
  • @Toothrot What Stephen just said, also there is very seldom any use for comments in interactive shells.
    – Kusalananda
    Dec 16, 2019 at 12:50
  • @Toothrot I've added a bit about what # is used for when it's not a comment.
    – Kusalananda
    Dec 16, 2019 at 12:59
  • 1
    @Clashsoft technically it is indeed a non-issue, but you’ve have to re-wire a few humans... Perhaps a more accurate way to present it would be “principle of least surprise”. My history shows quite a few # (typically in commit messages with bug numbers), I for one would be thrown a little if interactive_comments became the default in interactive shells. Dec 16, 2019 at 22:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .