Bash脚本printf';定义包含在变量中的转义序列

Bash脚本printf';定义包含在变量中的转义序列,bash,Bash,Bash代码: yellow="\e[1;33m" chosen_colour="${yellow}" declare -i score=300 printf '%s %d\n' "${chosen_colour}" "${score}" 结果: \e[1;33m 300 应该是: 300 /* in yellow */ 如何将包含ANSI转义序列的字符串值插入到printf语句中,而不使用以下任一语法: 避免1:(事实上,这是可行的,但在做大量的+=s时是浪费) 避免2:(在我的例子中

Bash代码:

yellow="\e[1;33m"
chosen_colour="${yellow}"
declare -i score=300
printf '%s %d\n' "${chosen_colour}" "${score}"
结果:

\e[1;33m 300
应该是:

300 /* in yellow */

如何将包含ANSI转义序列的字符串值插入到printf语句中,而不使用以下任一语法:

避免1:(事实上,这是可行的,但在做大量的
+=
s时是浪费) 避免2:(在我的例子中很难做到,因为需要这种构造的变量太多了)
我希望能够使用
printf
调用的参数部分,根据预定义的格式字符串传递要替换的值,就像我在第一个示例中天真地做的那样

我可以这样生活:

printf \
  '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s...' \
    "${a1}" \
    "${a2}" \
    "${a3}" \
    "${a4}" \
    "${a5}" \
    "${a6}" \
    ...
${!a*} # or similar
虽然最终,对于我的许多变量,我会使用如下构造:

printf \
  '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s...' \
    "${a1}" \
    "${a2}" \
    "${a3}" \
    "${a4}" \
    "${a5}" \
    "${a6}" \
    ...
${!a*} # or similar
你可以:

printf "^[%s foo" "${a1}" # that is ctrl+v, ESC, followed by %s
或:


对不起,我把“foo”从我原来的问题中删除了,因为我认为一个直接(简单)的例子会更清楚。如果我这样尝试你的例子:
printf“^[%s%d\n”“${selected\u color}”“${score}”
,它不起作用。@Robottinosino,printf
“\033%s%d\n”“${a1}”“${score}”
只想突出显示双引号,而不是单引号。这里我运行:
s=300;a1=“\e[1;33m”;a2=“${a1}”printf e%s%d\n}a2$s;
->
e[1;33m 300
您尝试过上面提供的两种解决方案吗?它们对我很有用。我不熟悉可以将
\e
转换为转义字符的shell,因此不要假设您的也可以。请注意第一种解决方案中关于Ctrl-V、Esc的注释。祝您好运。+1用于使用vt100 ish终端设置字符串!祝您好运。
printf "\033%s foo" "${a1}"  # 033 octal for ESC