shell - 在 shell 中使用 'list variables' 。如何为列表中的所有元素添加前缀?

标签 shell

我有一个(希望如此)简单的问题,我一直在尝试解决,但没有取得太大成功。 考虑以下简单的 shell 脚本:

列表=“ABC”

对于 $scripts 中的 i 做 //一些逻辑 完成

回显$列表

上述示例所需的输出是:

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/

相关文章:

linux - 使用 ls 查找满足条件的列表文件

php - linux 相当于使用 system() 从 PHP 在 Windows 上运行 .bat 文件

linux - 根据情况重新构建 shell 脚本

javascript - 如何通过网页按钮onclick执行linux脚本?

unix - 如何在 unix ksh shell 中比较某些产品的版本?

java - c3p0配置shell命令

regex - 在 grep 中转义斜杠 "\"

shell - 使用 Puppet 设置环境变量

C++ ShellExecute特殊字符

bash - 使用 Shell 脚本自动将 .tar 文件提取到当前目录