How can I make printf print unicode characters saved in a variable?

0

I have this code to print a horizontal line using unicode character \u2501:

#!/bin/tcsh
set horz_line = "'"
foreach x (`seq 1 1 80`)
   set horz_line = "${horz_line}\\u2501"
end
set horz_line = "${horz_line}\\n'"
printf $horz_line

But it is not giving what I expected. I expect the output to look like the one generated by below:

#!/bin/tcsh
foreach x (`seq 1 1 79`)
   printf '\u2501'
end
printf '\u2501\n'

The reason why I want to try out the code in the first block is because the code in second block works but is slow. When I run the second block code, I can see the whole line being drawn couple of characters at a time.

My thinking was that that's probably because printf is called 80 times. So I am trying out the first block approach where I generate a string of \u2501\u2501.. (80 times) and call printf just one to print that.

Kaushal Modi

Posted 2014-10-17T15:30:21.280

Reputation: 223

Answers

1

The following gets what I want in the first code block in the question. It is significantly faster than the second code block.

#!/bin/tcsh
set horz_line = ""
foreach x (`seq 1 1 80`)
    set horz_line = ${horz_line}'\\u2550' # double line
end
printf "`echo $horz_line`\n"
unset horz_line

Kaushal Modi

Posted 2014-10-17T15:30:21.280

Reputation: 223

Good find! For me it had to be one backslash: check="\u2718" then later printf "`echo -e $check`" – Heath Raftery – 2019-08-08T13:20:52.860