How do you echo a 4-digit Unicode character in Bash?

后端 未结 18 2052
醉梦人生
醉梦人生 2020-11-29 14:38

I\'d like to add the Unicode skull and crossbones to my shell prompt (specifically the \'SKULL AND CROSSBONES\' (U+2620)), but I can\'t figure out the magic incantation to m

18条回答
  •  被撕碎了的回忆
    2020-11-29 15:09

    Here's a fully internal Bash implementation, no forking, unlimited size of Unicode characters.

    fast_chr() {
        local __octal
        local __char
        printf -v __octal '%03o' $1
        printf -v __char \\$__octal
        REPLY=$__char
    }
    
    function unichr {
        local c=$1    # Ordinal of char
        local l=0    # Byte ctr
        local o=63    # Ceiling
        local p=128    # Accum. bits
        local s=''    # Output string
    
        (( c < 0x80 )) && { fast_chr "$c"; echo -n "$REPLY"; return; }
    
        while (( c > o )); do
            fast_chr $(( t = 0x80 | c & 0x3f ))
            s="$REPLY$s"
            (( c >>= 6, l++, p += o+1, o>>=1 ))
        done
    
        fast_chr $(( t = p | c ))
        echo -n "$REPLY$s"
    }
    
    ## test harness
    for (( i=0x2500; i<0x2600; i++ )); do
        unichr $i
    done
    

    Output was:

    ─━│┃┄┅┆┇┈┉┊┋┌┍┎┏
    ┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟
    ┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯
    ┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿
    ╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏
    ═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟
    ╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯
    ╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿
    ▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏
    ▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟
    ■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯
    ▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿
    ◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●
    ◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟
    ◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯
    ◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿
    

提交回复
热议问题