Padding characters in printf

前端 未结 13 819
半阙折子戏
半阙折子戏 2020-11-30 16:58

I am writing a bash shell script to display if a process is running or not.

So far, I got this:

printf \"%-50s %s\\n\" $PROC_NAME [UP]
13条回答
  •  情深已故
    2020-11-30 17:42

    echo -n "$PROC_NAME $(printf '\055%.0s' {1..40})" | head -c 40 ; echo -n " [UP]"
    

    Explanation:

    • printf '\055%.0s' {1..40} - Create 40 dashes
      (dash is interpreted as option so use escaped ascii code instead)
    • "$PROC_NAME ..." - Concatenate $PROC_NAME and dashes
    • | head -c 40 - Trim string to first 40 chars

提交回复
热议问题