Get the App
SLTechnology News&Howtos  ›  Servers  › 

Printf format

Shulou Source: shulou.com Published: 2022-06-03 05:33:12 09月21日 Update

Format:

printf format [string]

printf format parameter

Because printf behavior is defined by the POSIX standard, scripts using printf are more portable than those using echo.

Procedure:

#!/ bin/bash

printf "%-10s %-8s %-4s\n" Name Sex Weight kg

printf "%-10s %-8s %-4.2f\n" Guo Jingnan 66.1234

printf "%-10s %-8s %-4.2f\n" Yang Guonan 48.6543

printf "%-10s %-8s %-4.2f\n" Guo Fu Female 47.9876

# format-string is double quotes

printf "%d %s\n" 1 "abc"

#Single quotes work just like double quotes

printf '%d %s\n' 1 "abc"

Run Results:

Name Sex Weight kg

Guo Jingnan 66.12

Yang Guo Male 48.65

Guo Fu Female 47.99

1 abc

1 abc

Tags: Quotation marks format weight name gender parameters effects are made by standard portability program result script behavior migration run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Shulou Tech Info OPPO Reno Shulou Information