How do I print some text in bash and pad it with spaces to a certain width?

后端 未结 3 1559
春和景丽
春和景丽 2020-12-14 14:25

I\'m echoing some text in a bash script with a variable in it, and want to pad that variable so it will always have the appropriate ammount of spaces to the right to keep th

3条回答
  •  遥遥无期
    2020-12-14 14:46

    Use - to left align a field.

    printf "Echoing random number %-5s   [ OK ]" $RAND_NUM
    

    Alternatively, if you're on a Red Hat Linux system there are predefined functions that will print out green OK and red FAILED prompts (the ones you see during bootup):

    #!/bin/bash
    
    . /etc/init.d/functions
    
    echo -n "Frobbing widget:"
    frob_widget && echo_success || echo_failure
    echo
    

提交回复
热议问题