向字符串添加多个后缀

Add multiple suffixes to a string

我想将范围内的后缀附加到位于 file 中的字符串 foo。该文件保证只有一行字符串。

我已经能够在 Bash 中做到这一点:

for num in {001..003}; do
  echo $(cat file) $num
done

输出所需的:

foo 001
foo 002
foo 003

但我想知道是否有更好的方法可以不用 for 循环。

我试过类似的东西:

$ echo $(cat file) {001..003}
foo 001 002 003
$ echo {001..003} | cat file -
foo
001 002 003

您可以在 bash 中使用 printf:

s=$(<file)
printf "$s %s\n" {001..003}

foo 001
foo 002
foo 003

假设 file 的内容不包含任何 printf 格式化指令。