printf field width doesn't support multibyte characters?

前端 未结 6 503
孤独总比滥情好
孤独总比滥情好 2021-01-15 04:21

I want printf to recognize multi-byte characters when calculating the field width so that columns line up properly... I can\'t find an answer to this problem and was wonderi

6条回答
  •  灰色年华
    2021-01-15 05:00

    A pure shell solution

    right_justify() {
            # parameters: field_width string
            local spaces questions
            spaces=''
            questions=''
            while [ "${#questions}" -lt "$1" ]; do
                    spaces=$spaces" "
                    questions=$questions?
            done
            result=$spaces$2
            result=${result#"${result%$questions}"}
    }
    

    Note that this still does not work in dash because dash has no locale support.

提交回复
热议问题