Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash printf Command - DesignLinux

Dec 12 2020
designlinux 0 Comments
Bash printf

The printf command have more control over the formatting of the output. It formats and prints arguments similar to the C printf() function. In this article we will see how to use printf command bash.

printf Command#

Bash and other shells like Zsh and Ksh have printf by default. The standalone binary /usr/bin/printf is also available but shell built-in version is most preferred. Here, we will see the built-in version of printf

Following is the basic syntax of the printf command:

printf [-v var] format [arguments]

The -v option used to assign output to a variable instead of print the output.

In format you can pass string with following three types of objects:

  • Normal characters.
  • Backslash-escaped characters.
  • Conversion specifications

You can pass the n number of arguments. The format specifier will reuse if passed more arguments and consume the all arguments. If passed less arguments then it will supply the extra numeric-format are set to zero value while string-format specifiers are set to null string.

Below points are useful while passing arguments to the printf command:

  • The shell will consider all variables, wildcard matching, and special characters after passing the arguments to the printf command.
  • When using single quotes '' the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.
  • Following is the common example of using printf
printf "Pending Complaints: %s\nSolved Complaints: %s\n" "14" "24"
Pending Complaints: 14
Solved Complaints: 24

Here, the text Pending Complaints: %s\nSolved Complaints: %s\n is the format while "14" and "24" are arguments. There are two newline characters (\n) and two format specifiers (%s) that are replaced with the arguments.

The printf command doesn’t add a newline character (\n) at the end of the line.

Backslash-escaped Characters#

The backslash-escaped characters are interpreted when used in the format string or in an argument corresponding to a %b conversion specifier. Below is the list of the most common escape characters:

  • \ – To show a backslash character.
  • \b – Displays a backspace character.
  • \n – Show a new line.
  • \r – To display a carriage return.
  • \t – Displays a horizontal tab.
  • \v – Used to show a vertical tab.

Conversion specifications#

Below is the form of conversion specification:

%[flags][width][.precision]specifier

Every conversion specification stars with the percent sign (%). It includes optional modifiers and ends with letters that represent the data type (specifier) of the corresponding argument: aAbcdeEfgGioqsuxX.

Type conversion specifier#

To determine how to interpret the corresponding argument, need to use the type conversion specifier. It’s mendatory and must placed after the optional fields.

Below is the list of all type of conversions with it’s function:

  • %b – To print the argument while expanding backslash escape sequences.
  • %q – Used to print the argument shell-quoted, reusable as input.
  • %d, %i – Print the argument as a signed decimal integer.
  • %u – Use to print the argument as an unsigned decimal integer.
  • %o – It prints the argument as an unsigned octal integer.
  • %x, %X – Print the argument as an unsigned hexadecimal integer. %x prints lower-case letters and %X prints upper-case.
  • %e, %E – Print the argument as a floating-point number in exponential notation.
  • %a, %A – Print the argument as a floating-point number in hexadecimal fractional notation.
  • %g, %G – Print the argument as a floating-point number in normal .
  • %c – Prints the argument as a single character.
  • %f – It will print the argument as a floating-point number.
  • %s – Print the argument as a string.
  • %% – Print a literal % symbol.

An unsigned number represents zero and positive numbers, while a signed number represents negative, zero, and positive numbers.

The following command prints the number 50 in three different number systems:

printf "Decimal: %d\nHex: %x\nOctal: %o\n" 50 50 50
Decimal: 50
Hex: 32
Octal: 62

Flags directive#

Flags are used to set the leading zeros, prefixes, justification, etc. It’s an optional modifier.

Below are most used directives:

  • - – Used to align the text to left align. By default, the text is right-aligned.
  • + – It will prefix the numbers with a + or – signs. Negative numbers are prefixed with a negative sign by default.
  • 0 – To pads numbers with leading zeros instead of space.
  • blank – Prefix the positive numbers with a blank space and negative numbers with a minus (-).
  • # – An alternative format for numbers.

Width directive#

The width directive used after the flag characters to specify the minimum number of characters the conversion should result in.

If the width of the output text is less than the specified width, it will be padded with spaces. You can specify the width as a non-negative decimal integer or an asterisk (*).

For example:

printf "%15s %d\n" Kunj 908

In above example, the %15s means set the field at least 15 characters long. It will add the blanks the text because, by default, the output is right-justified. To align the text to left, use the – flag (%-15s).

     Kunj 908

Conclusion#

You learned how to use the printf command to print the formatted text.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: bash, terminal

How to Migrate CentOS 8 Installation to CentOS Stream

Prev Post

How to Redirect stderr to stdout in Bash

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 605
  • 1,055,377

DesignLinux.com © All rights reserved

Go to mobile version