bash字符串中心化处理

CENTRAL STRING


function central_line() {

    #输入字符串长度 
    str_length=${#1}
    
    #echo ${str_length}
    
    #中心线长度
    line_length=$(( (120-${str_length}) / 2 ))
    
    #echo ${line_length}
    
    line=`eval printf "=%0.s" {1..${line_length}}`
    
    #echo ${line}
    
    if ((str_length % 2)) ; then
    
        printf "${line} $1  ${line}
" 
    
    else
    
        printf "${line} $1 ${line}
"
    
    fi
    
}

central_line "hello world"
central_line "hello worldu"
central_line "lina how are you now"

原文地址:https://www.cnblogs.com/movit/p/14669262.html