I wonder how can I make a Celsius character in my bash script, so I could correct the output in "CPU temp: 50 C" line? What are the escape sequences for this kind of special characters, providing my font supports them?
Still scratching my head...
To be more precise I need it to use in conky I use this line: awk '{printf($3" C")}'. How should I correct it so that it outputs Celsius degree? Cause if I insert ℃ character directly, conky screws it.
In a shell, the most common way to escape special characters is to use a backslash before the characters. These special characters include characters like ?, +, $, !, and [. The other characters like ?, !, and $ have special meaning in the shell as well.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.
Bash now supports Unicode escape sequences using \u
:
echo -e "\u00b0"
will print
°
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"; }'
℃
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With