为每个命令行参数添加前缀字符串

问题描述 投票:0回答:2

我有一个要求,我将所有命令行参数存储在一个字符串中,(由于其他限制,不能将它存储在数组中)。

我的问题是,我想为存储在字符串“services”中的所有命令行参数添加前缀。

    services=$( echo $* | tr -d '/' )
    ---------------------------
    ??
    ---------------------------
    >./script.bash web studio

应该生成以下输出。

test-web test-studio
linux bash
2个回答
0
投票

使用sed命令,这应该足够了:

params=$(echo " $services" | sed "s/\ / $prefix/g")

然后只是'echo'params var


0
投票

在bash中,你可以这样做:

echo "test-"{web,studio}
test-web test-studio

如果a = web; b = studio:

echo "test-"{$a,$b}
test-web test-studio
© www.soinside.com 2019 - 2024. All rights reserved.