Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: color codes to eval



On Wed, Apr 10, 2024, at 12:46 PM, Ray Andrews wrote:
> var="${red}howdy${nrm}"    # '$(red}' is just a color code, no problem there.
> print -l $var
> eval "print -l $var"
>
> ... shows:
>
> howdy   
> (eval):1: bad pattern: ^[[31
>
> ... Is there a way to feed color codes to eval?

The issue is not "color codes" per se.  It's the fact that (assuming
ECMA-48 sequences) you're assembling and evaluating a command that
contains a word with an unquoted, unpaired "[" character, which zsh
deems to be an invalid glob (by default).  It's no different from:

	% print a[b
	zsh: bad pattern: a[b


> Seems to me I remember 
> that there's some ${(?) var} ... some flag in there that does it.

There are a number of possible solutions.

	# Avoid the pointless eval in the first place.
	print -l $var

	# Delay all expansions.
	eval 'print -l $var'

	# Delay the problematic expansion only.
	eval "print -l \$var"

	# Delay learning how quoting actually works.
	eval "print -l ${(q)var}"

	# Leave invalid patterns in the command.
	unsetopt BAD_PATTERN
	eval "print -l $var"


-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author