I can change my cursor shape like this:
printf "\x1b[\x31 q" # block :)
printf "\x1b[\x35 q" # ibeam :)
But when I assign that to a variable with %s
and expand using printf
, it doesn't work:
CURSOR="\x1b[\x3%s q"
printf ${CURSOR} 1 # block :(
printf ${CURSOR} 5 # ibeam :(
How do I fix this?
printf
sees are\x1b[\x3%s
,q
and1
(or5
). Sinceprintf
reuses format strings until the arguments are exhausted, the format string is printed twice, once withq
replacing%s
and once with1
(or5
). You should quote your variables there; otherwise it has a completely different meaning.printf
does not support hex backslash escapes? Your code is not portable as it relies on non-POSIX features.