The printf command is a built-in command in the Linux bash shell that is used to print standard input stdin text to the standard output stdout or a file. In contrast to echo, printf provides more control over the format and placement of the output.
This guide will provide a detailed explanation of the bash printf command with the following content.
- What is printf Command in Linux?
- How to Use printf Command in Bash?
- Backslash Escaped Characters
- Format/Conversion Specifiers
- Flag and Width Directives
Let’s start with the basics of the printf command.
What is printf Command in Linux?
The printf command displays the output in the format specified by the user. The printf command uses backslash-escaped characters, which modifies the output according to the specified option.
The printf command comes with the format or conversion parameters that format the output according to the used formatting option, such as string (%s) and others used to convert binary & decimal numbers. This command allows the users to specify the flag to set the width and number of digits in the output.
The syntax of the printf command is as follows:
$ printf [-v var] [format] [arguments]
- -v var: This option assigns the output to a variable.
- format: Specify the format in which the output will be printed.
- arguments: Replace it with the statement on which format will be applied.
How to Use printf Command in Bash?
This section will explain the printf command with its options and other available parameters.
Backslash Escaped Characters
These characters are used to format the output with the printf command. These characters interrupt the output to display in a specified manner. The below-written backslash-escaped characters are available in the printf command:
\\ | Prints a backslash character. |
\n | Shows output on a new line. |
\t | Provide the tab space horizontally. |
\b | Shows the backspace to delete a single character. |
\r | Shows the carriage return. |
\v | Provide the tab space vertically. |
Let’s use these characters as examples.
Example 1: Print on New Line to Standard Output
To print a string on the new line from the standard input stdin to the standard output stdout, the “\n” character of the printf command is used. For instance, the below script has a “\n” character on at 3 points which will print on new lines:
#!/bin/bash
printf "Hi!\nWelcome to this guide!\nitslinuxfoss here!\n"
For checking the output, run the script using the below command:
$ bash test.sh
The output shows that “\n” initiates the new line and prints the output on the new line.
Example 2: Print by Providing a Horizontal Tab Space
The “\t” option of the printf command is used to provide a horizontal (left to right) “tab” space. For instance, the below script uses “\t” at 2 points which will provide a “tab” space:
#!/bin/bash
printf "Hi!\tWelcome to this guide\titslinuxfoss here\n"
For checking the output, run the script using the below command:
$ bash test.sh
The output shows a “tab” space at specified points.
Example 3: Print by Providing a Vertical Tab Space
The “\v” provides a vertical “tab” space on the output. The following script will have “\v” at 2 points will show the vertical tab space:
#!/bin/bash
printf "Hi!\vWelcome to this guide\vitslinuxfoss here\n"
For checking the output, run the script using the below command:
$ bash test.sh
The output shows the vertical tab space from the previous output.
Example 4: Print by Removing a Single Character
The “\b” is used as the backspace character from the keyboard, which deletes a single character. To delete a single character from the output with the “\b” option at 2 instances, the following script is used:
#!/bin/bash
printf "Hi!\bWelcome to this guide\bitslinuxfoss here\n"
For checking the output, run the script using the below command:
$ bash test.sh
The output verifies that the “!” space from the first point and the “e” word from the second point is removed using the “\b” option of the printf command.
Format/Conversion Specifiers
The format or conversion specifiers use the percentage (%) character that formats or converts the output according to the specified option. The below-mentioned options are available for the printf command:
%% | Prints output as % literal. |
%c | Prints output as single line character. |
%s | Prints output as a string. |
%d | Prints output as a decimal integer. |
%f | Prints output as the floating-point number. |
%x | Prints output as a hexadecimal integer. |
%o | Prints output as an octal integer. |
Let’s understand format specifiers with examples.
Example 1: Print a String Using Printf Command
The “%s” format of the printf command treats the output as a string. For instance, a variable “name” is declared, which is used with “%s” of the printf command to print the output as a string data type:
Note: If a number is used with the “%s”, it will convert the number data type to string.
#!/bin/bash name="itslinuxfoss"
printf "Welcome, %s\n" $name
For checking the output, run the script using the below command:
$ bash test.sh
Example 2: Redirect Arguments to File Using Printf Command
The printf command is used with the user-input arguments, which can be redirected to a file. For instance, the below script takes the user input “name”, “Location”, and “age”. It redirects them to the file “testfile.txt”:
#! /bin/bash
echo "Your Name:"
read name
echo "Your Location:"
read loc
echo "Your Age:"
read age
printf "Your Name is: %s\nYour Location is: %s\nYour Age is: %s\n" "$name" "$loc" "$age" >> testfile.txt
For checking the output, run the script using the below command:
$ bash test.sh
The output of the printf command using the arguments can be saved to a specific file using the “>>” redirect operator which can be verified from “testfile.txt” using the below “cat” command:
$ cat testfile.txt |
The output displays that the user-input arguments are redirected to the “testfile.txt”.
Example 3: Print a Number Using Printf Command
The “%d” character shows the output as a decimal, while “%x” shows as the hexadecimal integer type. For instance, the below script will display the number “35”, as a decimal and hexadecimal integer:
#!/bin/bash
printf "The Decimal number is %d\nThe Hexadecimal number is: %x\n" 35 35
For checking the output, run the script using the below command:
$ bash test.sh
The output shows the decimal and hexadecimal conversion numbers for “35”.
Example 4: Convert Decimal to Octal Numbers Using printf Command
We can convert the decimal to the octal number using the “%o” option of the printf command. For instance, the below script will convert the decimal number entered by the user to octal number:
#!/bin/bash
printf "Enter Your Desired Decimal Number:\n"
read num
printf "The Octal of "$num" is %o\n" "$num"
For checking the output, run the script using the below command:
$ bash test.sh
The user inputs the number “68”, which is converted to “104” as an octal number.
Example 5: Print a Number to Specific Decimal Points
We can specify the number of decimal points to print the output using the “%f” option. The below code will print the “pi” variable values to 2 decimal points:
#!/bin/bash
pi=3.141592
printf "The Pi is: %.2f.\n" $pi
For checking the output, run the script using the below command:
$ bash test.sh
The output is shown as 2 decimal points.
Flag and Width Directives
The width directive controls the width of the output, while the flags are used to modify the output by adding prefixes. The flags and width parameters below are available in the printf command:
– | Width modification from the left side. |
+ | Width modification from the right side. |
0 | Prefixes the numbers with 0 instead of space. |
blank | Prefix with blank space for positive numbers while negative (–) symbol for negative numbers. |
Let’s work with these flags and width directives.
Example 1: Print the Data with Specific Width
We can control the width of the output using the “+” or “–” options. For instance, the below bash script will provide the output as 15 characters long, and if the characters are shorter than 15 characters, it adds the blank space:
Note: The +15 means the space will be from the left side, while the -15 means the space will be from the right side.
#!/bin/bash
printf "%+15s\n" 10 "Hello"
For checking the output, run the script using the below command:
$ bash test.sh
The output adds the blank spaces to make it 15 characters long.
Example 2: Print the Data with Prefix 0
We can print the output for a specific number of integers in it and if the number does have all the integers then the blank space or “0” can be prefixed. For instance, the below bash shell script will make the number “5” to 5 characters long number by prefixing the number of zeros:
#!/bin/bash
printf "%0*d\n" 5 5
For checking the output, run the script using the below command:
$ bash test.sh
The output shows the number “5” as five characters long by adding 4 zeros before it.
That’s all from the printf bash command.
Conclusion
The bash printf command in Linux prints the output with specific formatting by using the backslash-escaped characters, conversion or format specifiers, or width directives. The printf command provides every option to format the output and control the width & number of characters, as discussed in this guide.