InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Writing Output with More Formatting Control in bash
Bash

Writing Output with More Formatting Control in bash

InfinityCoder January 12, 2017

You want more control over the formatting and placement of output.

Use the printf built-in command.
For example:

1
2
3
$ printf '%s = %d\n' Lines $LINES
Lines = 24
$

or:

1
2
3
$ printf '%-10.10s = %4.2f\n' 'GigaHerz' 1.92735
GigaHerz = 1.93
$

The printf built-in command behaves like the C language library call, where the first argument is the format control string and the successive arguments are formatted according to the format specifications (%).
The numbers between the % and the format type (s or f in our example) provide additional formatting details.

For the floating-point type (f), the first number (4 in the 4.2 specifier) is the width of the entire field.

The second number (2) is how many digits should be printed to the right of the decimal point. Note that it rounds the
answer.
For a string, the first digit is the maximum field width, and the second is the minimum field width.

The string will be truncated (if longer than max) or blank padded (if less than min) as needed.

When the max and min specifiers are the same, then the string is guaranteed to be that length.

The negative sign on the specifier means to left-align the string (within its field width). Without the minus sign, the string would right justify, thus:

1
2
3
$ printf '%10.10s = %4.2f\n' 'GigaHerz' 1.92735
  GigaHerz = 1.93
$

The string argument can either be quoted or unquoted. Use quotes if you need to preserve embedded spacing (there were no spaces needed in our one-word strings), or if you need to escape the special meaning of any special characters in the string (again, our example had none).

It’s a good idea to be in the habit of quoting any string that you pass to printf, so that you don’t forget the quotes when you need them.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using Just the Beginning or End of a File in bash
You need to display or use just the beginning or …

Using Just the Beginning or End of a File in bash

Counting Lines, Words, or Characters in a File in bash
You need to know how many lines, words, or characters …

Counting Lines, Words, or Characters in a File in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap