0

Why, after using set histchars='?', is there an error that occurs every time I source ~/.zshrc?

/home/user/.zshrc:1: bad pattern: #

In other words, every # token within comments is causing some issues.

5
  • Please update your question and include 1) the line from your .zshrc file that caused the issue, 2) the output from setopt, and 3) the command you ran (the one you show would cause an error). I would expect that error to occur if you paste code containing comments into an interactive zsh shell. That is the default behaviour of zsh (see e.g. Allowing comments in interactive zsh commands). Sourcing a script should still work, and changing the histchars variable to a single ? should give you a command not found error instead.
    – Kusalananda
    May 20, 2023 at 8:27
  • @Kusalananda Assuming the code is actually histchars='?' without set or setopt, your (currently deleted) answer is correct. May 21, 2023 at 9:33
  • @Gilles'SO-stopbeingevil' It would generate a "command not found" error, not a "bad pattern" error, though. It's unclear what this user is actually doing. I closed the question as they had had time to ask a new question without caring for clarifying this one.
    – Kusalananda
    May 21, 2023 at 9:35
  • @Kusalananda It would be a bad pattern error if setopt extended_glob is also present, which is very likely. May 21, 2023 at 9:44
  • @Gilles'SO-stopbeingevil' Thanks. I'll double-check and update my answer. I will keep the question closed as it's still unclear what the user actually did. EDIT: I'm reopening the question to allow you to post your own answer alongside min if you wish to do so.
    – Kusalananda
    May 21, 2023 at 9:47

1 Answer 1

1

The command that you show ought to give you the following error message:

setopt: no such option: histchars=?

It should have no other effect.

However, if you set the histchars variable to the single character ?, then you disable the use of comments in the shell. This is because the zsh shell uses the third character of the histchars variable as the comment indicator.

The variable is documented like this in the zshparam(1) manual:

Three characters used by the shell's history and lexical analysis mechanism. The first character signals the start of a history expansion (default !). The second character signals the start of a quick history substitution (default ^). The third character is the comment character (default #).

The characters must be in the ASCII character set; any attempt to set histchars to characters with a locale-dependent meaning will be rejected with an error message.

Setting histshars=? would provoke a command not found error for each comment character where a command is usually allowed (like at the start of a line). If you have set the EXTENDED_GLOB shell option in the shell, a bad pattern error would be generated instead.

If you want to change the initial character of histchars, the history expansion indicator, to a question mark, use

histchars[1]=?

You must log in to answer this question.