假设我调用一个带有3个参数的脚本,a,abc和xyz,这样$@包含这三个参数.
假设我想调用write命令:
command fooa fooabc fooxyz bara barabc barxyz
我怎么做到这一点?
我不认为{foo,bar} $@或{foo,bar} {$@}工作,因为大括号扩展发生在$@扩展之前.
解决方法
您可以使用:
command "${@/#/foo}" "${@/#/bar}"
这使用shell parameter expansion的替换变体.#将匹配锚定到参数的开头.
$set -- a abc xyz $echo command "${@/#/foo}" "${@/#/bar}" command fooa fooabc fooxyz bara barabc barxyz $
Is there a way to scale this for larger
{foo,bar,baz,...}$@
or{foo,bar}{123,456}$@
?
是的,但你最终会使用数组:
$set -- a abc xyz $args=( "$@" ) $for prefix in foo bar baz who why; do prefixed1+=( "${args[@]/#/$prefix}" ); done $echo command "${prefixed1[@]}" command fooa fooabc fooxyz bara barabc barxyz baza bazabc bazxyz whoa whoabc whoxyz whya whyabc whyxyz $
或者(注意你必须正确排序):
$for prefix in 123 456; do prefixed1+=( "${args[@]/#/$prefix}" ); done $for prefix in foo bar; do prefixed2+=( "${prefixed1[@]/#/$prefix}" ); done $echo command "${prefixed2[@]}" command foo123a foo123abc foo123xyz foo456a foo456abc foo456xyz bar123a bar123abc bar123xyz bar456a bar456abc bar456xyz $
请注意,这会保留参数中的空格:
$unset prefixed1 prefixed2 $set -- 'a b' 'x y z' $args=( "$@" ) $for prefix in 'p p-' '123 456 '; do prefixed1+=( "${args[@]/#/$prefix}" ); done $echo command "${prefixed1[@]}" command p p-a b p p-x y z 123 456 a b 123 456 x y z $for prefix in 'foo 1-' 'bar 2-'; do prefixed2+=( "${prefixed1[@]/#/$prefix}" ); done $echo command "${prefixed2[@]}" command foo 1-p p-a b foo 1-p p-x y z foo 1-123 456 a b foo 1-123 456 x y z bar 2-p p-a b bar 2-p p-x y z bar 2-123 456 a b bar 2-123 456 x y z $printf "[%s]\n" "${prefixed2[@]}" [foo 1-p p-a b] [foo 1-p p-x y z] [foo 1-123 456 a b] [foo 1-123 456 x y z] [bar 2-p p-a b] [bar 2-p p-x y z] [bar 2-123 456 a b] [bar 2-123 456 x y z] $
第一个循环可能是:
for prefix in 'p p-' '123 456 '; do prefixed1+=( "${@/#/$prefix}" ); done
不使用${args [@]}数组.