0

A basic rule of zsh is that you don't need to quote your variables, for example:

% data="single argument"
% print -l $data
single argument

One exception I know of is that if an argument must not be skipped even if empty, it must be double quoted:

% emptyarg=
% functon count() { echo $# }
% count $emptyarg
0
% count "$emptyarg"
1

However, if an argument contains certain special characters, it must be quoted or the shell gives an error. Why is that? It seems fragile. The content of my data shouldn't substantially influence how functions/commands run.

1 Answer 1

1

Answering my own question-- I noticed the behavior works as expected in scripts (parameters don't expand, even without quotes). I realized the issue was probably caused by an option, so I ran setopt in my interactive shell and in a script and compared the results. I turned them off one by one until I found setopt noglobsubst had the desired effect. In short, the GLOB_SUBST option makes zsh treat all variables as patterns to be expanded if possible. You can disable this option and still expand variables when needed with the tilde ~ parameter expansion:

% setopt noglobsubst
% star=*
% echo $star
*
% echo $~star
readme.txt test.sh
2
  • is the globsubst option on by default? Commented Jun 17, 2019 at 4:17
  • @UncleBilly No, I must have turned globsubst on in the zsh new user install, because it was recommended online, or because I was just mistaken about what the option did. Commented Jun 18, 2019 at 2:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.