Bash printf Operate: 7 Examples for Linux

If you've been using the bash shell for a long time, you probably know how to use the echo command to print strings in the Linux terminal. However, the printf command makes printing text with certain formatting a lot easier.

Today we're learning how to use the printf function to improve our bash scripting skills.

What is the bash printf function?

As the name suggests, printf is a function that prints formatted text strings. That is, you can write a string structure (the format) and later fill it with values ​​(the arguments).

If you are familiar with the C / C ++ programming languages, you may already know how printf works. Printf in the bash shell is very similar, but with a few small differences.

More Information: Best Web Sites to Get Started With C ++ Programming

Using printf in the bash shell

The basic syntax for printf is:

printf format (argument)

Printf prints the format String during implementation escapes ((.) and Guidelines ((%.) by using specified Arguments. Notice the output of the following command:

$ printf "Hello,% s" world
Hello World

Printf took the world Argument and replaced the % s Characters in the specified string with it.

You may find that Bash doesn't create a new line for you after the output. Unlike the echo command, printf doesn't assume that you want a new line after printing the output. Therefore you have to use the new line character n In any case.

Enter to print a string, then move to a new line printf "Hello world n".

Hello World

It is important to note that printf tries to reuse the format for all of your arguments. You can use this to your advantage, but if you aren't careful you can get the following results:

$ printf "My name is% s" Jordan Gloor
My name is Jordan. My name is Gloor

You should also be aware that printf interprets all statements as if an argument is missing 0 (for a number) and zero (for a string).

$ printf "Hello,% s."
Hello, .

Now that you know how bash scripting works with printf on Linux, you can use it in your commands.

Linux bash printf examples

Printf has many format specifiers that you can use in your scripts. But today we're only going to cover a few common ones for the function.

1. Format the output with a string

Use the key to format the output with text strings % s Directive.

$ printf "% s is one of the largest% s online." MUO "Technology Publications"
MUO is one of the largest online technology publications.

2. Format the output string with decimal places

You can use the to format a string with an integer % d Directive for a signed decimal place.

$ printf "MUO was founded in% d." 2007
MUO was founded in 2007.

Note that if you want to use an unsigned decimal place in the output, you must use that % u Directive instead.

Related: What Does "Bash" Mean in Linux?

3. Output math functions with printf

Formatting math functions using the printf command is easy. Just put your expression in double brackets, as you normally do in Bash, and specify the expression in the argument list.

$ printf "1 + 1 is% d" $ ((1 + 1))
1 + 1 is 2

4. Format hexadecimal numbers

If you need to format a hexadecimal number, use % x for lowercase letters and % X. for capital letters.

$ printf% X 1000
3E8C

To print the current date and time, you can combine printf with the date Command and pass the following instructions.

$ printf "% (% m-% d-% Y% H:% M:% S) T" $ (date +% s)
03/26/2021 3:27:57 PM

You can see the format specifiers for month, day, year, hour, minute, and second in the above output.

6. Format strings with Unicode characters

Use the key to print unicode characters with printf u Escape for 16-bit Unicode and U. for 32-bit Unicode.

For example, you can print them Copyright © Icon with the following command:

$ printf " u00A9"
©

7. Add a gap to the output

You can format your strings with spacing by adding a minimum number of printed characters in front of the directive identifier. Unused characters are filled with spaces.

For example typing printf% s:% 5d n value 1 25 value 2 120 is issued:

Value 1: 25
Value 2: 120

The % 5d The format specifier in the output indicates that the formatted string contains at least five characters. Since 25 has only two digits, spaces are added to the remaining characters.

If you pass a negative number as the width, the directive is left-justified rather than right-justified.

$ printf% -10s:% d "Circles" 25 "Boxes" 120
Circles: 25 boxes: 120

If you want to make the width variable, you can pass an asterisk instead of a number and printf uses the next number it finds in the argument list.

$ printf "% * s:% d" -10 "Circles" 25 -10 "Boxes" 120
Circles: 25 boxes: 120

Related: Everything You Need to Know About Bash For Loops on Linux

Learning the Linux printf command

You can use the printf command in your bash script utility to print complex and diverse strings in your terminal. When developing your bash scripts, it is important to make sure that you are working in the most productive environment possible. Not only does this improve your command line skills, it also improves your workflow in general.

10 best Linux distributions for developers

Ready to start developing on an open source operating system? Here are the best Linux distributions for programming.

Continue reading

About the author

Jordan Gloor
(32 articles published)

Jordan is a tutor and journalist who is passionate about making Linux accessible and stress-free for everyone. He has a BA in English and a thing for hot tea. During the warm season he enjoys cycling on the Ozark hills where he lives.

More
By Jordan Gloor

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

Your email address will not be published. Required fields are marked *