right text align - bash

前端 未结 3 538
广开言路
广开言路 2020-12-11 06:25

I have one problem. My text should be aligned by right in specified width. I have managed to cut output to the desired size, but i have problem with putting everything on ri

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 06:34

    Try:

    printf "%40.40s\n" "$line"
    

    This will make it right-aligned with width 40. If you want no truncation, drop .40 (thanks Dennis!):

    printf "%40s\n" "$line"
    

    For example:

    printf "%5.5s\n" abc
    printf "%5.5s\n" abcdefghij
    printf "%5s\n" abc
    printf "%5s\n" abcdefghij
    

    will print:

      abc
    abcde
      abc
    abcdefghij
    

提交回复
热议问题