Shell's printf output statement

Source: Internet
Author: User



Summarize:



(1) printf uses reference text or space-delimited parameters, which can be used to format strings in printf, as well as the width of strings, left and right alignment, and so on. Default printf does not automatically add line breaks like echo, we can add \ n manually.



(2)%-10s refers to a width of 10 characters (-The left alignment, not the right alignment), any character will be displayed in 10 characters justifies, if not enough is automatically filled with spaces, more than will also show the content.



The printf command mimics the printf () program in the C library.



(3) formatted output, plus double quotation marks and no double quotation marks is the difference between, if the input is more, without double quotation marks, then the output, if the double quotation marks, the additional participants in the same format output;



Standard, so scripting with printf is better than using echo portability.



printf uses reference text or space-delimited parameters, which can be used outside of the format string in printf, as well as the width of the string, left and right alignment, and so on. Default printf does not automatically add line breaks like echo, we can add \ n manually.



The syntax of the printf command:


printf  Format-string [arguments...]     


Parameter description:


    • format-string: Controlling strings for formatting
    • arguments: is a list of parameters.


Examples are as follows:


"Hello, Shell"Hello,shell"Hello, shell\n"Hello,shell$  


Next, I'll use a script to demonstrate the power of printf:


#! / bin / bash
# author: Novice tutorial
# url: www.runoob.com
 
printf "% -10s% -8s% -4s \ n" Name Gender Weight kg
printf "% -10s% -8s% -4.2f \ n" Guo Jing male 66.1234
printf "% -10s% -8s% -4.2f \ n" Yang Guo Male 48.6543
printf "% -10s% -8s% -4.2f \ n" Guo Fu Female 47.9876 


Execute the script with the output as follows:


Name Gender Weight kg
Guo Jing Male 66.12
Yang Guo Male 48.65
Guo Fu Female 47.99 


%s%c%d%f are format alternates



%-10s refers to a width of 10 characters (-for left alignment, not to right-aligned), any character will be displayed in 10 character justifies characters, if not enough is automatically filled with spaces, more than will also show the content.



%-4.2F refers to the format of decimals, where. 2 refers to 2 decimal places reserved.






More examples:


#! / bin / bash
# author: Novice tutorial
# url: www.runoob.com
 
# format-string is double quoted
printf "% d% s \ n" 1 "abc"

# Single quotes have the same effect as double quotes
printf ‘% d% s \ n’ 1 "abc"

# Can be output without quotes
printf% s abcdef

# The format only specifies one parameter, but the extra parameters will still be output according to the format, and format-string will be reused.
printf% s abc def

The result is abcdef
printf "% s \ n" abc def

The result is
abc
def
Formatted output, the difference between double quotes and no double quotes is that if there are more input parameters, without double quotes, it is output as it is. If double quotes are added, the extra input parameters will be output in the same format;
printf "% s% s% s \ n" a b c d e f g h i j

# If there are no arguments,% s is replaced with NULL and% d is replaced with 0
printf "% s and% d \ n" 



Execute the script with the output as follows:


 
1 abc 1 abc
abcdefabcdefabc def a b c
d e f
g h i
j and 0
Escape sequences for printf
sequence Description
\a A warning character, usually an ASCII bel character
\b Back off
\c Suppresses (does not display) the newline character at any end of the output (only valid in the parameter string under the control of the%b format designator), and any characters left in the argument, any subsequent arguments, and any characters left in the format string are ignored
\f Page Change (formfeed)
\ n Line break
\ r Enter (carriage return)
\ t Horizontal tab
\v Vertical tab
\\ A literal backslash character
\ddd A character that represents an octal value of 1 to 3 digits. Valid only in format strings
\0ddd Represents octal value characters from 1 to 3 bits
Instance
printf "a string, no processing: <% s> \ n" "A \ nB"
a string, no processing: <A \ nB>

$ printf "a string, no processing: <% b> \ n" "A \ nB"
a string, no processing: <A
B>

$ printf "www.runoob.com \ a"
www.runoob.com $ # does not wrap 


Shell's printf output statement


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.