On Tue, Jun 29, 2010 at 03:29:17AM -0700, Linda Walsh wrote: > I often have situations where I want to split an existing var into > 2 or more sub values. I usually use some combination of > ${VAR<#|%>[<#|%>]expr}, > but as soon as I don't check my break-down, I screw it up.
IFS='#%' read -r -a array <<< "$string" > One would be the 'split' with an "expr" to split on, and a Var. > "Expr" would usually be 1 single character, but making it an > expression would make it more flexible. Multi-byte delimiters get ugly very, very fast. You can't use IFS for those. Personally I would recommend moving up to awk/perl if you need to deal with such cases. > I'd like to see the ability to > specify a list of recipient vars, with the last being a "catchall" Like what read does? :-) > that would become an array if there were multiple values still needing > L-value locations. I.e.: > > (a b)=(1 2 3) > > would assign 1 to 'a', and 'b' would become an array, with b[0]=2, b[1]=3. Oh... not quite like read, then. But this is an extremely bizarre request. Assigning a scalar *and* an array in the same command, with no syntactic differentiation between the two... wow. > Sure, I can do all these things manually with effort, but these would > simplify some common operations. Common? Hah. Anyway, if you really wanted to do the above, it would look something like: IFS='whatever' read -r -a b <<< "1 2 3" a=${b[0]} unset 'b[0]' Now you have a "sparse" array (indexed from 1 to N instead of 0 to N) and a scalar variable containing the former first element. At the risk of assuming too much, I can't really imagine this being a "common" requirement. For the most part, if you've got a list of options or something where the first one is special, you simply pass the whole option list to the command in question, and let IT shift off the first option, instead of doing so yourself. Or, if YOU are the command in question, you get the option list (argv) and you write something like: specialarg=$1 shift Et voila.