Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: parameter expansion question
- X-seq: zsh-users 5752
- From: "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh users list <zsh-users@xxxxxxxxxx>
- Subject: Re: parameter expansion question
- Date: Sun, 19 Jan 2003 06:33:06 +0000
- In-reply-to: <20030118233214.43327.qmail@xxxxxxxxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20030118233214.43327.qmail@xxxxxxxxxxxxxxxxxxxxxxx>
On Jan 18, 6:32pm, Le Wang wrote:
}
} I can force a variable to be expanded by using
} ${~var}.
The only difference between ${var} and ${~var} is that in the latter
case the resulting value is subject to filename generation. It does
not "force" anything -- if filename generation would not normally
occur in the context, using ${~var} will not cause it to happen.
Perhaps you understood that already, but it's a commom mistake.
} Is there any way I can force a string to be
} expanded without assigning it to a variable first?
What does "be expanded" mean to you? I thought at first you were
talking about filename generation, but then you said:
} I'm after the same information for filename generation.
This question is almost meaningless. In most contexts, strings are
subject to filename generation. In contexts where filenames are not
generated, assigning to a variable won't help.
If "be expanded" means you want to use other parameter operations
such as ${var%pattern} on the string: Yes, you can do that, like
this:
zsh% print ${${:-some string here}% here}
some string
} Right now, if I want to see if the glob pattern
} '(#i)${i%.rar}*.par' would match anything, I would do:
}
} parFile=`print (#i)${i%.rar}*.par`
And then what? "Want to see if [it] would match" could mean several
things. It could mean you want want to test whether there are any
matches, e.g. [[ -n $parFile ]], or that you just want to print out
what the matches are so you can look at them, or ...
I suspect you mean that you want to find out whether, for each file
matching "*.rar", there exists a file with the same root name and
the extension ".par" -- but I've no idea what you want to do with
that information once you have it. Perhaps this will help:
print (#i)*.rar(e:'REPLY=${REPLY%.rar}.par &&
[[ -f $REPLY ]] || reply=()':)
} if I use:
}
} parFile=(#i)${i%.rar}*.par
}
} Zsh thinks I want an array.
There are several things going on here.
Firstly, unless you use `setopt glob_assign', zsh doesn't do filename
generation during assignment to scalar variables, so you wouldn't get
what you expect there anyway.
Secondly, it is true that there's an ambiguity with using parens for
glob modifiers and using them to wrap array assignments. This should
probably be considered a bug (agree, PWS?) which can be worked around
by using the "${:-string}" trick I showed above:
parFile=${:-(#i)${i%.rar}*.par}
Finally, even if you use the workaround with glob_assign set, whether
the variable is set as a string or as an array depends on whether the
glob matches one or more than one file. So you're probably better
off using an array to begin with, that is:
parFiles=( (#i)${i%.rar}*.par )
which requires neither glob_assign (because filename generation IS
always done during array assignments) nor any strange workarounds.
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net
Messages sorted by:
Reverse Date,
Date,
Thread,
Author