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?

  • 1
    Possible duplicate of Why does my shell script choke on whitespace or other special characters? or any of the questions linked to Security implications of forgetting to quote a variable in bash/POSIX shells. In short, quote your variables!. – Stéphane Chazelas Apr 19 '19 at 19:45
  • @StéphaneChazelas, Where am I missing double quotes here?! It would have been awesome if this is fixed with a double quotes; Or do you mean quote in some general sense? – Nishant Apr 19 '19 at 19:49
  • 1
    Please read those Q&As – Stéphane Chazelas Apr 19 '19 at 19:53
  • 1
    In the second set of commands, the arguments printf sees are \x1b[\x3%s, q and 1 (or 5). Since printf reuses format strings until the arguments are exhausted, the format string is printed twice, once with q replacing %s and once with 1 (or 5). You should quote your variables there; otherwise it has a completely different meaning. – muru Apr 19 '19 at 20:20
  • 1
    Do you know that printf does not support hex backslash escapes? Your code is not portable as it relies on non-POSIX features. – schily Apr 23 '19 at 16:06

\x31 is the character 1 and \x35 is 5, so in this case you don't have to use escape sequences.

CURSOR="\x1b[%s q"

printf "${CURSOR}" 1
printf "${CURSOR}" 5
| improve this answer | |
  • I thought I knew a little bit of escaping, now I understand that it is an ocean :-). – Nishant Apr 20 '19 at 9:43

By using \x3%s, only \x3 is interpreted as an escape sequence, the %s is no longer part of it. If you want to make that part an argument, you'll have to use %b and bring out the whole \x31 to have that interpreted as an escape sequence:

$ printf "\x1b[\x31 q" | od -c
0000000 033   [   1       q
$ printf "\x1b[%b q" "\x31" | od -c
0000000 033   [   1       q
| improve this answer | |
  • Thanks for showing how to use debugging tools likeod. I was struggling to print these characters on the screen :-) – Nishant Apr 20 '19 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.