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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIt 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?
$ setopt interactive_comments
$ echo hello # comment
hello
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:
COMMENTS
In non-interactive shells, or in interactive shells with theINTERACTIVE_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
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
hello
$ shopt -u interactive_comments
$ echo hello # hello
hello # hello
#
is used for when it's not a comment.
#
(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