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

Re: Odd behavior with various (q) array modifiers and non-printable characters (backspace, newline)

On Wed, Aug 11, 2021, at 5:16 PM, Bart Schaefer wrote:
> You need to stop testing things with "echo".  The "echo" builtin
> interprets some backslash escapes itself, which will confuse you about
> what the quoting options have done.
> Repeat all your tests instead with
>   printf "%s\n" ${(q)...}
> and so on, and come back if you still have questions.

Additionally, \b and \n are not interpreted in double quotes, so
your initial data does not actually contain BS or NL characters.
Presumably you thought "..." works like $'...'.

% mysed_orig=( gsed "s/\ba\b/x/" )
% typeset -p mysed_orig
typeset -a mysed_orig=( gsed 's/\ba\b/x/' )

% mysed_fixed=(gsed $'s/\ba\b/x/')
% typeset -p mysed_fixed
typeset -a mysed_fixed=( gsed $'s/\C-Ha\C-H/x/' )

% newline_orig=( aa "b\nb" cc )
% typeset -p newline_orig
typeset -a newline_orig=( aa 'b\nb' cc )

% newline_fixed=( aa $'b\nb' cc )
% typeset -p newline_fixed
typeset -a newline_fixed=( aa $'b\nb' cc )

As per the QUOTING section of zshmisc(1):

	Inside double quotes (""), parameter and command substitution
	occur, and `\' quotes the characters `\', ``', `"', `$',
	and the first character of $histchars (default `!').


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