10

Is there a way to print the ASCII charachter '├── ' and '└──' using a bash or perl script? I want it be exactly like the output of 'tree'.

[root@localhost www]# tree -L 1
.
├── cgi-bin
├── error
├── html
└── icons
Tom Iv
  • 409
  • 1
  • 5
  • 21

3 Answers3

5

They look to be a part of the extended ascii codes. You can see them here http://www.asciitable.com/

Also available at the Unicode table here: http://unicode-table.com/en/sections/box-drawing/

enter image description here

I believe 192, 195 and 196 are the ones you are after. In Unicode 2501, 2514 and 2523.

EDIT

Found a related stack question here, which recommends printing in Unicode.

What you want is to be able to print unicode, and the answer is in perldoc perluniintro. You can use \x{nnnn} where n is the hex identifier, or you can do \N{...} with the name:

perl -E 'say "\x{2514}"; use charnames; say "\N{BOX DRAWINGS LIGHT UP AND RIGHT}"'
Community
  • 1
  • 1
Rots
  • 5,506
  • 3
  • 43
  • 51
4
echo -e "\0342\0224\0224\0342\0224\0200\0342\0224\0200 \033[01"
Undo
  • 25,519
  • 37
  • 106
  • 129
liyaoshi
  • 189
  • 4
4

Using Unicode specific character in

There are 3+ different answers here...

1. Using special character directly into UTF8 script.

HorizT='├'
HLine='─'
echo $HorizT$HLine$HLine
├──

2. Finding character value from output sample

As you are speaking tree as output sample, U could run something like

tree / | sed -ne '2{p;q}'
├── bin

tree / | sed -ne '2{p;q}' | od -An -t o1
342 224 234 342 224 200 342 224 200 040 142 151 156 012

To output your reference as octal values.

Knowing that Unicode use variable number of bytes, that your reference string hold 3 unicode characters, there look like 3 group of 3 values, begining by 342.

So you could try:

printf "\342\224\234\n"
├

printf -v HorizontalT "\342\224\234"
printf -v Hline "\342\224\200"

echo $HorizontalT$Hline$Hline$Hline
├───

3. Searching for specific character by using charmap

You could use dedicated Unicode browser, like charmap under Debian Gnome GNU/Linux:

GNOME Character Map

Browse and find your character, then on second tab: Character Details:

charmap detail

You may read Various Useful Representations -> octal escaped:

printf '\342\224\234\n'
├

3+. Printing Unicode fonts using printf

Once you know unicode value, you could use printf for creating variable:

printf -v char '\U251C'
echo $char
├

From there, after some brainstorm:

string=
for i in 0 2 16 24 {12..60..8} ;do
    printf -v r '\\U25%02X' $i
    printf -v char "$r"
    string+="$char "
done
echo "$string"
─ │ ┐ ┘ ┌ └ ├ ┤ ┬ ┴ ┼ 

Or

string=
for i in {80..108} ;do
    printf -v r '\\U25%02X' $i
    printf -v char "$r"
    string+="$char "
done
echo "$string"
═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ 

4. Final bash function

Strictly answering SO request:

I want it be exactly like the output of 'tree'.

Here is a function:

tree-L1() { 
    local _i indent=0 entry root=${1:-.};
    local -i dirs files;
    [[ $1 == -i ]] && indent=$2 && shift 2;
    echo "$root";
    . <(cd "$root";set -- *;echo ${@@A});
    printf -v indent '%*s' "$indent" '';
    for ((_i=1; _i<=$#; _i++))
    do
        entry=${!_i};
        [[ -d $root/$entry ]] && dirs+=1 || files+=1;
        [[ -L $root/$entry ]] && printf -v entry '%s -> %s' "${!_i}" "$(
            readlink "$root/${!_i}")";
        if ((_i==$#)); then
            printf '%b%b%b%b %s\n' "${indent// /\\U2502   }" \
                \\U2514 \\U2500{,} "$entry";
        else
            printf '%b%b%b%b %s\n' "${indent// /\\U2502   }" \
                \\U251C \\U2500{,} "$entry";
        fi;
    done;
    printf '\n%d directories, %d files\n' $dirs $files
}

Then you could try to compare:

diff <(tree-L1 /etc) <(tree -L 1 /etc)

may output nothing, as there's no diffs! Or

diff --width 80 -y <(tree-L1 /etc) <(tree -L 1 /etc)

5. Browse Unicode table by character name

Please have a look at Is there a (linux) terminal character picker? on SuperUser, I've posted a little Python unicode library:

./dumpUnicode | grep 'BOX DRAWINGS LIGHT.*\(HORI\|VERT\)'
\U002500: '─' BOX DRAWINGS LIGHT HORIZONTAL
\U002502: '│' BOX DRAWINGS LIGHT VERTICAL
\U002504: '┄' BOX DRAWINGS LIGHT TRIPLE DASH HORIZONTAL
\U002506: '┆' BOX DRAWINGS LIGHT TRIPLE DASH VERTICAL
\U002508: '┈' BOX DRAWINGS LIGHT QUADRUPLE DASH HORIZONTAL
\U00250A: '┊' BOX DRAWINGS LIGHT QUADRUPLE DASH VERTICAL
\U00251C: '├' BOX DRAWINGS LIGHT VERTICAL AND RIGHT
\U002524: '┤' BOX DRAWINGS LIGHT VERTICAL AND LEFT
\U00252C: '┬' BOX DRAWINGS LIGHT DOWN AND HORIZONTAL
\U002534: '┴' BOX DRAWINGS LIGHT UP AND HORIZONTAL
\U00253C: '┼' BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL
\U00254C: '╌' BOX DRAWINGS LIGHT DOUBLE DASH HORIZONTAL
\U00254E: '╎' BOX DRAWINGS LIGHT DOUBLE DASH VERTICAL
\U01FBAF: '' BOX DRAWINGS LIGHT HORIZONTAL WITH VERTICAL STROKE
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137