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

Re: triviality regarding $# counts



On Sat, Apr 13, 2024, at 5:01 PM, Bart Schaefer wrote:
> On Sat, Apr 13, 2024 at 1:36 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>>
>> On 2024-04-13 12:45, Bart Schaefer wrote:
>> > redline () { echo -e "$red$*$nrm" }
>> >
>> So, compared to the previous expansion, how would '$*' expand there?
>
> redline a b c
>
> would mean
>
> echo -e "$red$1 $2 $3$nrm"
>
> One word, with two quoted spaces, instead of three words.
>
>> I'm unclear as to the practical  difference.
>
> For "echo" it doesn't matter because echo is also going to paste its
> arguments together, but it might matter to some other command.

A different output method should make this clearer:

	% f1() { printf '<%s>' "x$@x"; echo; }
	% f2() { printf '<%s>' "x$*x"; echo; }
	% f1 a b c
	<xa><b><cx>
	% f2 a b c
	<xa b cx>

The command "printf '<%s>' ..." outputs each argument enclosed in
"<...>" (other than the format string itself).  Observe that
double-quoted $@ can expand to multiple words, while double-quoted
$* always expands to one.

This actually makes a difference to nearly every command you might
think of, with "echo" being a rare exception (and even then, only
when the first character of IFS is a space).

	% arr=(.zshenv .zprofile .zshrc)
	% set -x
	% wc -c "$arr[@]"
	+zsh:44> wc -c .zshenv .zprofile .zshrc
	    1063 .zshenv
	    1023 .zprofile
	    3807 .zshrc
	    5893 total
	% wc -c "$arr[*]"
	+zsh:45> wc -c '.zshenv .zprofile .zshrc'
	wc: .zshenv .zprofile .zshrc: open: No such file or directory


-- 
vq




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