8

Let the script below exemplify my quandary..

#!/bin/zsh

STUFF=( moose-hoof ovary clydsedale )

echo ${MINE=$(printf "MY-%s " $STUFF)}
echo ${MINE_EXP=${STUFF/^/MY-}}

MY-moose-hoof MY-ovary MY-clydsedale

moose-hoof ovary clydsedale

What are the right expansion flags to allow string concatenation on each element of the array?

16

Use $^array.

It turns the array into a sort of brace expansion of the array. As in when a=(foo bar baz), $^a would be a bit like {foo,bar,baz}.

$ a=(foo bar baz)
$ echo prefix${^a}suffix
prefixfoosuffix prefixbarsuffix prefixbazsuffix

For multiplexing arrays:

$ a=(1 2 3) b=(a b c)
$ echo $^a$^b
1a 1b 1c 2a 2b 2c 3a 3b 3c

Naturally, if the prefix or suffix contain shell special characters (like ; that separates commands or space that separate words, or $"'&*[?~...), they must be quoted:

echo 'p r e f i x '$^a' s u f f i x'

same as for csh's (and bash, ksh, zsh's):

echo 'p r e f i x '{foo,bar,baz}' s u f f i x'

$^a itself must not be quoted, "foo${^a}bar" would expand as one word. One case where you would want $^array to be quoted, the same as for $array is when you want to preserve empty elements. Then, you need to quote the array expansion and use the (@) flag or the "${array[@]}" syntax (reminiscent of the Bourne shell's "$@"):

$ array=(x '')
$ printf '<%s>\n' $array         # empties removed
<x>
$ printf '<%s>\n' "$array"       # array elts joined with spaces
<x >
$ printf '<%s>\n' "${(@)array}"  # empties preserved
<x>
<>
$ printf '<%s>\n' "$array[@]"    # empties preserved
<x>
<>
$ printf '<%s>\n' $^array$^array # empty removed
<xx>
<x>
<x>
$ printf '<%s>\n' "$^array$^array" # concatenation of joined arrays
<x x >
$ printf '<%s>\n' "$^array[@]$^array[@]" # multiplexing with empties preserved
<xx>
<x>
<x>
<>
  • Unfortunately this isn't working for me.. echo "number ${^$(seq 3)} " -> number 1 2 3 or Z=( $(seq 3) ); echo "number ${^Z}" -> number 1 2 3 – alex gray Jan 31 '16 at 21:34
  • @don_crissti Only after trying your latter suggestion, echo "number "${^$(seq 3)}"" would it work. i have NEVER seen double quotes inside double quotes like that. VERY weird. VERY hard to remember.. but thanks! – alex gray Jan 31 '16 at 22:50
  • @alexgray it's not quotes within quotes, it's the prefix and suffix being quoted, but not ${^(seq 3)}, see edit. – Stéphane Chazelas Feb 1 '16 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.