Shell Programming - Printf

Unix

In this tutorial we will learn about printf command in Shell Programming.

About printf

The printf command is used to print pre-formatted output. And it is similar to the printf() function of C programming language.

We can say that printf is a successor of echo command.

printf syntax

printf <format> <arguments>

Where, format is the format string used on the arguments.

format is optional and can be omitted.

Example #01

In the following example we will print name and score entered by the user.

#!/bin/sh

# take user name
printf "Enter name: "
read name

# take user score
printf "Enter score: "
read score

# display result
echo "---Result---"
printf "Name: %s\n" "$name"
printf "Score: %d\n" "$score"

Output:

$ sh example01.sh 
Enter name: Yusuf Shakeel
Enter score: 8
---Result---
Name: Yusuf Shakeel
Score: 8

Where, \n prints the new line i.e., take cursor to the next line.

%s is a format for string. %d is a format for integer.

Format

Following are the format we can use for the arguments in printf command.

FormatDescription
%dFor signed decimal numbers
%iFor signed decimal numbers
%uFor unsigned decimal numbers
%oFor unsigned octal numbers
%xFor unsigned hexadecimal numbers with lower case letters (a-f)
%XFor unsigned hexadecimal numbers with upper case letters (A-F)
%fFor floating point numbers
%sFor string
%%For percent % symbol

Modifiers

We use modifiers with format to modify output.

FormatDescription
NThis specifies the width of the field for output.
*This is the placeholder for the width.
-To left align output in the field. (Default: Right align)
0Pad result with leading 0s.
+To put + sign before positive numbers and - sign for negative numbers.

Example #02

In the following example we will print 100 in decimal, octal and hexadecimal form.

#!/bin/sh

num=100

# output in decimal, octal, hex form
printf "Decimal: %d\n" "$num"
printf "Octal: %o\n" "$num"
printf "Hex: %X\n" "$num"

Output:

$ sh example02.sh 
Decimal: 100
Octal: 144
Hex: 64

If we want to add leading 0 for octal numbers and 0x for hex then we will add # as shown below.

#!/bin/sh

num=100

# output in decimal, octal, hex form
printf "Decimal: %d\n" "$num"
printf "Octal: %#o\n" "$num"
printf "Hex: %#X\n" "$num"

Output:

$ sh example02-1.sh 
Decimal: 100
Octal: 0144
Hex: 0X64

Example #03

In the following example we will take two integer numbers from the user and divide them and show 5 decimal place.

#!/bin/sh

# get the first number
printf "Enter first integer number: "
read a

# get the second number
printf "Enter second integer number: "
read b

# if b is 0
if [ $b == 0 ]
then
  printf "Division by 0 not allowed.\n"
  exit	# exit the script
fi

# perform division
result=`expr "$a / $b" | bc -l`

printf "%d / %d = %.5f\n" "$a" "$b" "$result"

Output:

$ sh example03.sh 
Enter first integer number: 5
Enter second integer number: 2
5 / 2 = 2.50000

$ sh example03.sh 
Enter first integer number: 5
Enter second integer number: 0
Division by 0 not allowed.

In the above code we are first take value of a and b from user.

Then, we are checking value of b. If b is 0 then we are printing an error message and quitting the script using exit command. And we are performing this check using the if statement.

Then, we are dividing a by b and using bc command with -l option to get decimal values and storing the result in result variable.

And then we are printing the result using the printf command.

The format is "%d / %d = %.5f\n" where, the first %d if for a, the second %d is for b.

And %.5f is for the result. Note! .5 meaning we want 5 decimal place in the result.

Example #04

In the following example we will divide a by b and we will let the user decide the number of decimal places for the result.

#!/bin/sh

# get the first number
printf "Enter first integer number: "
read a

# get the second number
printf "Enter second integer number: "
read b

# get the decimal place
printf "Enter decimal place for the result: "
read d

# if b is 0
if [ $b == 0 ]
then
  printf "Division by 0 not allowed.\n"
  exit	# exit the script
fi

# perform division
result=`expr "$a / $b" | bc -l`

printf "%d / %d = %.*f\n" "$a" "$b" "$d" "$result"

Output:

$ sh example04.sh 
Enter first integer number: 5
Enter second integer number: 2
Enter decimal place for the result: 1
5 / 2 = 2.5


$ sh example04.sh 
Enter first integer number: 5
Enter second integer number: 2
Enter decimal place for the result: 3
5 / 2 = 2.500


$ sh example04.sh 
Enter first integer number: 5
Enter second integer number: 2
Enter decimal place for the result: 0
5 / 2 = 2

In the above script we have %.*f in the format string. The * is replaced by the $d which tell us about the number of decimal place in the final result.