9

How would I print these characters in Linux?

│ (ascii 179)

├ (ascii 195)

└ (ascii 192)

─ (ascii 196)

I cannot find any octal values that would work with echo -e "\0xxx", any ideas?

Benjamin
  • 2,718
  • 5
  • 21
  • 20

4 Answers4

8

After much poring over man printf and info printf, I think I've gotten this to work.

The basic issue seems to be that bash has a built-in printf that doesn't work. And, despite what the man/info pages, say, \U doesn't work. \u still does, though.

env printf '\u2502'

gets me a vertical box character.

drysdam
  • 8,341
  • 1
  • 20
  • 23
  • 1
    `env printf '\u2502'` gets me "â", maybe there are some settings I would need to change?.. I was hoping to find a solution that would work in any environment – Benjamin Apr 07 '11 at 19:30
  • 4
    The full list of box drawing characters for Unicode to be used with this, are listed in http://en.wikipedia.org/wiki/Box-drawing_character – MV. Jun 28 '12 at 22:52
  • Rather than using "env printf" you can just run "/bin/printf" so that Bash runs the external printf program, not the builtin one (which does not supports this unicode syntax). BTW, this will only works with printf from GNU coreutils, it may not work in other Unix versions. – MV. Jun 28 '12 at 23:00
  • Well, `env printf` is not yet the whole of the story. You can also prepend some locale before the `env` to take influence on results, e. g. `LC_ALL=en_GB.UTF-8 env printf '\u2502'`. I think that's the problem that Benjamin has faced. To find the source of these issues, it's always very recommendable to type `locale` in console to see what the current settings are. (Particular ones will also take influence on the language of some X11 apps.) – syntaxerror Jul 04 '15 at 02:32
6

You can use the exact same codes you provided or of the extended ASCII character set (e.g. 195 for ├) if you've got the right encoder to display the characters.

On Linux, we lack the non-standard extended ASCII character set support - which is why it's not displayed. However, I found another character set that's available for Linux and is almost similar to the extended ASCII character set. It's IBM855.

All you have to do is changed the character encoding of your command line application to IBM855. All popular box drawing characters have the same code of the extended ASCII character set - which is the most important.

You may compare the sets by this image and this image.

PS: If you're using gnome-terminal, you can add IBM855 charset by clicking the "Terminal" menu from the menu bar -> "set character encoding" -> "Add or Remove". Look for IBM855, and add it. Now just choose the encoding from "terminal"->"set character encoding"->"Cyrillic (IBM855)".

They boxes were enough for my homework. Hope this helps. :)

AnxiousNut
  • 61
  • 1
  • 3
2

Either switch the font to one that is in PC-8/CP437 encoding, or use the Unicode values for those characters instead, encoded into the current charset.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Because some people may still want to know this...

See the lines that uses iconv to translate.

To print all ascii/extended ascii codes CP437 in Linux/bash script:

# heading index with div line
printf "\n      "; # indent

for x in {0..15}; do printf "%-3x" $x; done;
printf "\n%46s\n" | sed 's/ /-/g;s/^/      /';

# two lines with dots to represent control chars
c=$(echo "fa" | xxd -p -r | iconv -f 'CP437//' -t 'UTF-8')
printf "%32s" | sed 's/../'"$c"'  /g;s/^/  0   /;s/$/\n\n/'
printf "%32s" | sed 's/../'"$c"'  /g;s/^/  1   /'

# convert dec to codepage 437 in a table
for x in {32..255};
do

  # newline every 16 translated code values
  (( x % 16 == 0 )) && printf "\n\n"

  # left index numbers
  let "n = x % 15"
  (( (x % 16) == 0 )) && printf "%-4x" $n | sed 's/0/f/;s/^/  /'

  # conversion of x integer value to symbol
  printf "%02x" $x | xxd -p -r | iconv -f 'CP437//' -t 'UTF-8' | sed 's/.*/&  /'

  # div line
  (( x == 127 )) && printf "%46s" | sed 's/ /-/g;s/^/      /;i\ '

done
printf "%46s" | sed 's/ /-/g;s/^/\n      /;s/$/\n      /'; # div line
for x in {0..15}; do printf "%-3x" $x; done;
echo

shmatt
  • 31
  • 1
  • Thank-you @shmatt! It works perfectly. [example](https://tio.run/##lVJNS8NAFLzvrxiWpJsIuy9fbYWQk@DZg96CUPNhA7IpTZGA@tvjJmmaIqJkT8syszNv3rzsmn3XHY6VPpXgqcZweMzK@ogWlcaHp5S//oqR15hwtgxbDqvtH3URs5lvR5sm1RyfaIocoiGQpNe4oWcavyYRsyyxnCLb1@Dlroe2bQ55gDyae5XV@h2yhLh7iMItkYA8QTw93stb4V6U7DBorlSUIsGtjAsjMKl5vZq5W5TqVJNYwvUHp1chhIFSwdrEwPKaAY5j3m34GySJEXJdrFaYUzAJGNBbcQLXSEbomo88ZyS6vzFtGfW56tmcRyWdPRk/mJFeMGxgSXpXI9/Q6vzhMImx4gfbn2bMKv9aZFylEKwvAPuXMhVr2selC1hWtL41XfcN) – roblogic May 28 '20 at 05:31