我有一个(希望如此)简单的问题,我一直在尝试解决,但没有取得太大成功。 考虑以下简单的 shell 脚本:
列表=“ABC”
对于 $scripts 中的 i 做 //一些逻辑 完成
回显$列表
上述示例所需的输出是:
block 引用>right-A right-B right-C
最佳答案
使用printf
并利用其re-using the format if extra arguments are given的功能:
# specifically, do not quote the variable so that
# the shell can split it into words
printf "right-%s " $list
# the printf command does not emit a newline, so
# do it here
echo
将其捕获为新变量,假设是 bash:
printf -v prefixed_list "right-%s " $list
关于shell - 在 shell 中使用 'list variables' 。如何为列表中的所有元素添加前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14494548/