How to make special characters in a Bash script for conky? How to make special characters in a Bash script for conky? bash bash

How to make special characters in a Bash script for conky?


All non-control sequences of bytes your script outputs to the terminal are interpreted for display according to the terminal's settings. If you configure it to interpret incoming data as utf-8 then all you need to do in your bash script is output the Celsius character as required by utf-8.

Setting your terminal right depends on the application you're using. It is quite likely that it is already in utf-8 mode.

In order to display the character you want you need to look up its Unicode codepoint and encode it in utf-8 or you can look it up somewhere where character's utf-8 sequence is already shown. Celsius character is described here for example.

Celsius utf-8 sequence is 0xE2 0x84 0x83, so you can display it in your shell using the $'string' construct since it accepts the \xhh escape:

$ echo $'\xe2\x84\x83'℃

or echo with -e:

$ echo -e '\xe2\x84\x83'℃

Also, you can assign it to a variable

$ CEL=$'\xe2\x84\x83'$ echo $CEL℃

If you wish to display ℃ as two separate characters: degree character and the C letter then you can use this character and this command:

$ echo $'\xc2\xb0'C°C

If you're using a shell which doesn't support the $'string' construct, you can also use utilities like perl or python:

$ python -c 'print "\xe2\x84\x83"'℃$ perl -e 'print "\xe2\x84\x83\n"'℃

In GNU awk you can use standard C language escapes including \xhh, for example:

$ awk 'BEGIN { print "\xe2\x84\x83"; }' ℃$ awk 'BEGIN { print "\xc2\xb0\C"; }' °C

Note that an extra backslash was needed to terminate the hexadecimal number (without it the escape sequence consumes all hexadecimal digits including letter C, see GNU awk user's guide).

More portable solution in awk is to use octal sequence. Since hexadecimal 0xC2 is octal 302 and hexadecimal 0xB0 is octal 260, you can do the following:

$ awk 'BEGIN { print "\302\260C"; }'°C

Similarly,

$ awk 'BEGIN { print "\342\204\203"; }'℃


Bash now supports Unicode escape sequences using \u:

echo -e "\u00b0"

will print

°