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语句中:
+=
时这很浪费)s="${yellow}"
s+="${score}"
s+=...
s+=...
s+=...
s+=...
s+=...
s+=...
s+=...
s+=...
printf "${yellow}${score}${a1}${a2}${a3}${a4}${a5}${a6}${a7}${a8}........."
我希望能够根据预定义的FORMAT字符串传递要替换的值,使用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
答案 0 :(得分:3)
你这样做:
printf "^[%s foo" "${a1}" # that is ctrl+v, ESC, followed by %s
或:
printf "\033%s foo" "${a1}" # 033 octal for ESC