RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Bash printf syntax basics

Article Index
1. Format specifiers
2. Examples

When writing a bash scripts most of us by default use echo command as means to print to standard output stream. echo is easy to use and mostly it fits our needs without any problem. However, with simplicity very often comes limitation. This is also the case with echo command. Formatting an echo command output can be a nightmare and very often impossible task to do.

The solution to this can be a good old friend of all C/C++ the “printf” tool. printf can be just as easily implemented into a bash script is it is used with C/C++ programs. This article describes some basics of printf along with practical examples:

Syntax

printf accepts a FORMAT string and arguments in a following general form:

printf  

In format prinft can have format specifiers, escape sequences or ordinary characters. When it comes to arguments it is usually text we would like to print to standard output stream. Let’s start with something simple from a bash shell command line:

 

$printf “hello printf”
hello printf$

At this point we have supplied and argument “hello”. Not the different behaviour in comparison to echo command. No new line had been printed out as it it in case of when using default setting of echo command. To print a new line we need to supply printf with format string with escape sequence \n ( new line ):

$ printf "%s\n" "hello printf"
hello printf

The format string is applied to each argument:

$ printf "%s\n" "hello printf" "in" "bash script"
hello printf
in
bash script

1. Format specifiers

As you could seen in the previous simple examples we have used %s as a format specifier. The most commonly used printf specifiers are %s, %b, %d, %x and %f . The specifiers are replaced by a corresponding arguments. See the following example:

$ printf "%s\t%s\n" "1" "2 3" "4" "5"
1       2 3
4       5

In the example above we have supplied two specifiers %s to print TAB ( \t ) and NEWLINE ( \n ) to be used a s part of printf format string to print along with each argument. First \t is applied to argument “1” and \n is applied to argument “2 3”. If there are more arguments than specifiers the format string is reused until all arguments had been depleted. Specifier %s means to print all argument in literal form.

2. Examples

As we have now covered the very basics let’s see some more printf examples: Instead of %s specifiers we can use %b specifier which is essentially the same by it allows us to interpret escape sequences with an argument:

$ printf "%s\n" "1" "2" "\n3"
1
2
\n3
$ printf "%b\n" "1" "2" "\n3"
1
2

3
$

When it comes to printing a integers we can use %d specifier:

$ printf "%d\n" 255 0xff 0377 3.5
255
255
255
bash: printf: 3.5: invalid number
3

As you can see %d specifiers refuses to print anything than integers. To printf floating point numbers a %f specifier is our friend:

$ printf "%f\n" 255 0xff 0377 3.5
255.000000
255.000000
377.000000
3.500000

The default behaviour of %f printf specifier is to print floating point numbers with 6 decimal places. To limit a decimal places to 1 we can specify a precision in a following manner:

$ printf "%.1f\n" 255 0xff 0377 3.5
255.0
255.0
377.0
3.5

Formatting to three places with preceding with 0:

for i in $( seq 1 10 ); do printf "%03d\t" "$i"; done
001     002     003     004     005     006     007     008     009     010

Simple table. Format names to 7 places nad max 7 characters and format floating point number to 9 places with 2 decimals. More complicated sample script using printf formatting to create a table with multiple items. Save as a script make executable and run:

#/bin/bash
divider===============================
divider=$divider$divider

header="\n %-10s %8s %10s %11s\n"
format=" %-10s %08d %10s %11.2f\n"

width=43

printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"

printf "%$width.${width}s\n" "$divider"

printf "$format" \
Triangle 13  red 20 \
Oval 204449 "dark blue" 65.656 \
Square 3145 orange .7

Output:

$ ./table 

 ITEM NAME   ITEM ID      COLOR       PRICE
===========================================
 Triangle   00000013        red       20.00
 Oval       00204449  dark blue       65.66
 Square     00003145     orange        0.70

Share this linux post:

Submit Bash printf syntax basics in Delicious Submit Bash printf syntax basics in Digg Submit Bash printf syntax basics in FaceBook Submit Bash printf syntax basics in Google Bookmarks Submit Bash printf syntax basics in Stumbleupon Submit Bash printf syntax basics in Technorati Submit Bash printf syntax basics in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download