Examples of Bash Printf Command
Here you will find out:
- about bash printf command and its syntax
- about special printf characters and their meanings
- what examples of using a bash printf function are
- when DiskInternals can help you
Are you ready? Let's read!
What is a bash printf command?
This is the built-in bash command for formatted output. The printf commands replaces the familiar echo command, which is limited in its capabilities. In addition, different versions of Unix have had different versions of it, which leads to incompatibilities. This property often forces you to use the newer printf command.
Information about syntax of shell script printf
Keep in mind that older versions of bash do not always support the printf command. The printf command prints an argument to standard output, using a specific format.
The general syntax is as follows:
Here is a bash printf example of its construction:
Where "Last name: %s\nName: %s\n" is in TeX format and two variables are passed as arguments pointed to by %s in the formats line (1 format specifier printf = 1 argument).
The structure of format string
All the components of a format string can be divided into 3 parts:
- ordinary characters for subsequent direct output
- several consecutive characters, often indicated by a character /
- format specifications to express the next argument
Special printf characters and their meanings
Each such character has its own special purpose and is interpreted only in the format %b:
- \c - End the output in the same way as escaping \c as in echo -e.
- The characters \', \" and \? are not deleted.
- All octal numbers starting with \0 can hold up to four digits.
Examples of using a bash printf function
There are various ways to use the bash printf function. The most common bash printf examples are mentioned here:
- If you need to print some text, you need to quote it; for example,
- If you want to avoid external lines for this line, use %s; for example,
- If you want to enter not one, but several parameters for printing according to the principle of the cylinder, this can be done; for example, like this:
- If you need to enter an integer value (for example age), then you can do it with %d; for example,
As you can see, this command has a lot of possibilities, and, if you delve deeper into this issue, it will become even more interesting.
Try to open Linux files from Windows
Here we are talking about the professional-quality and unique DiskInternals Linux Reader. You need to use it if you are using a virtual machine or dual-boot setup, and you need to get files from Linux to Windows. You just need to install this application once and don’t worry about transferring data from one OS to another. The program works with most file systems: Ext2/3/4, ReiserF, Reiser4, HFS, HFS +, FAT, exFAT, NTFS, ReFS, UFS2, and ZFS, XFS, Hikvision NAS and DVR (preview only).
DiskInternals Linux Reader transfers files using the Wizard and this is done at no cost. There is read-only access to files on Linux disks, leaving the source files unchanged, which indicates the precautions taken. In addition, before transferring data, you can preview and make sure of the results. Perhaps the most pleasant bonus is the function of creating a disk image, also for free. Therefore, DiskInternals Linux Reader should be your desktop application and reliable assistant.