In Bash we can already do this:
echo foo.{a,b,c}
# == foo.a foo.b foo.c
How do we get roughly:
arr=(a b c)
echo foo.{${arr[@]}}
# == foo.a foo.b foo.c
In Bash we can already do this:
echo foo.{a,b,c}
# == foo.a foo.b foo.c
How do we get roughly:
arr=(a b c)
echo foo.{${arr[@]}}
# == foo.a foo.b foo.c
You can use parameter expansion
$ arr=(a b c)
$ echo "${arr[@]/#/foo.}"
foo.a foo.b foo.c
"${arr[@]/@()/foo.}"
(with extglob
) which would make it also work in ksh93 (where the ${param/pattern/replacement}
operator comes from).
– Stéphane Chazelas
Jul 16 '20 at 07:57
"${arr[@]/+()/foo.}"
, or "${arr[@]/?()/foo.}"
in ksh/bash, but all will fail in zsh. @StéphaneChazelas
–
Jul 18 '20 at 18:51
@(x)
, +(x)
and ?(x)
are ksh glob operators whose zsh equivalents are (x)
, x##
and (x|)
. To use those ksh glob operators in zsh
, you'd set the kshglob
option, though you'd probably only do that as part of the ksh emulation to interpret code intended for ksh
.
– Stéphane Chazelas
Jul 18 '20 at 20:07
In case you don't have to use bash
:
rc
/es
/akanga
(that's the default behaviour):
$ arr=(a b c)
$ echo foo.$arr
foo.a foo.b foo.c
zsh
:$ arr=(a b c)
$ echo foo.$^arr
foo.a foo.b foo.c
Or
$ set -o rcexpandparam
$ arr=(a b c)
$ echo foo.$arr
foo.a foo.b foo.c
(^
enables rcexpandparam
for that one expansion, like =
enables shwordsplit
, or ~
globsubst
)
(also the default behaviour)
$ set arr a b c
$ echo foo.$arr
foo.a foo.b foo.c
All those shells have a better array design than bash's (itself copied from ksh)).
Note that zsh
and fish
expansion works like brace expansion. In rc
, it differs when using echo $arr.$arr
, which gives:
a.a b.b c.c
while in fish
/zsh -o rcexpandparam
, it gives the same as echo {a,b,c}.{a,b,c}
, that is:
a.a a.b a.c b.a b.b b.c c.a c.b c.c