printf

From Initq

Jump to: navigation, search


Print command displays a message on the screen. This command is mostly used in bash shall scripts.

Contents

\n

new line.

%d

Display decimal.

[root@initq test]# printf "%2.2d\n" 5
05
[root@initq test]# printf "%3.2d\n" 5
 05
[root@initq test]# printf "%3.20d\n" 5
00000000000000000005
[root@initq test]# printf "%3.30d\n" 5
000000000000000000000000000005
[root@initq test]# printf "%3.40d\n" 5
0000000000000000000000000000000000000005
[root@initq test]# printf "%3.50d\n" 5
00000000000000000000000000000000000000000000000005
[root@initq test]# printf "%3.80d\n" 5
00000000000000000000000000000000000000000000000000000000000000000000000000000005
[root@initq test]# printf "%3.100d\n" 5
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005
[root@initq test]# printf "%'d\n" 10000
10,000
[root@initq test]# printf "%'d\n" 100000
100,000
[root@initq test]# printf "%'d\n" 1000000
1,000,000
[root@initq test]# printf "%'d\n" 10000000
10,000,000

%s

[root@initq test]# export ERRMSG="warning: This Is An Error Message"
[root@initq test]# printf "%s\n" "$ERRMSG" | tr [:lower:] [:upper:]
WARNING: THIS IS AN ERROR MESSAGE
[root@initq test]# tr [:lower:] [:upper:] <<< "$ERRMSG"
WARNING: THIS IS AN ERROR MESSAGE

%f

qais@qais:~$ printf "%f\n" 5
5.000000
qais@qais:~$ printf "%.2f\n" 5
5.00
qais@qais:~$ printf "%.3f\n" 5
5.000
qais@qais:~$ printf "%.4f\n" 5
5.0000
Personal tools