Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 437
mountpeaks Avatar asked Nov 30 '11 23:11

mountpeaks


People also ask

How do I put special characters in a bash script?

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.

What does [- Z $1 mean in bash?

$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.

What is $@ in bash?

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.

What is echo $$ in bash?

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.


2 Answers

Bash now supports Unicode escape sequences using \u:

echo -e "\u00b0"

will print

°
like image 136
Jaap Joris Vens Avatar answered Oct 22 '22 09:10

Jaap Joris Vens


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"; }'
℃
like image 45
Adam Zalcman Avatar answered Oct 22 '22 08:10

Adam Zalcman