Shell printf Command

Source: Internet
Author: User
Tags echo command

In the previous section we learned about the Shell's echo command, which we'll learn from the Shell's other output command printf.

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

printf is defined more by the POSIX standard, so a script that uses printf is better than using echo to move the value.

printf uses reference text or space-delimited parameters, which can be used to format strings in printf, as well as to set the width-to-left alignment of strings, and so on.

Default printf does not automatically add line breaks to echo, and 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:

$ echo "Hello,shell"

Hello,shell

$ PRINRF "hello,shell\n"

Hello,shell

$

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

#! /binbash

# Author: Rookie Tutorial

# url:www.runoob.com

printf "%-10s%-8s%-4s\n" name sex weight kg

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

Execute the script with the output as follows:

Name Sex weight kg

Guo Jingnan 66.12

%s%c%d% is a format substitute

%-10s refers to a width of 10 characters (-for left alignment, not to right-aligned), any character will be displayed in 10 characters justifies, if not enough to automatically fill with a space

, the content will be displayed in full.

%-4 2f refers to the formatting of decimals, where,. 2 means reserved 2 is a decimal.

More examples:

#!/bin/bash

# Author: Rookie Tutorial

# url:www.runoob.com

# fotmat-sting is double quotes

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

# single quotes are the same as double quotes

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

# No quotation marks can also be output

printf%s abcdef

# The format specifies only one parameter, but the extra parameter is still output in that format, format-string is reused

printf%s ABC def

printf "%s\n" ABC def

printf "%s%s%s\n" a B c E F g h

# If there is no arguments, then%s is replaced with Hull,%d replaces with 0

printf "%s and%d \ n"

Execute the script with the output as follows:

Escape sequences for printf

Sequence description

\a warning character, usually the bel character of ASCII

\b Back

\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)

\ nthe line break

\ r Enter (carriage return)

\ t Horizontal tab

\v Vertical Tab

\ \ A literal backslash character

\DDD represents a 1 to 3-digit octal character that is valid only in a format string

\0DDD represents 1 to 3-bit octal value characters

Notes

%d%s%c%f format override

D:decimal decimal integer corresponding positional parameter must be a decimal integer, otherwise error!

s:string string corresponding positional parameter must be a string or character type otherwise error

C:char character corresponding positional parameter must be a string or character type otherwise error

F:float floating-point corresponding positional parameter must be numeric or error

For example, the last parameter is "def"%c automatically intercepts the first character of a string as the result output.

Shell printf Command

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.