The easiest way to print on the Linux command line is to use the echo command.
echo The value of var is $var
However, the echo command is not sufficient if it is necessary to print the formatted output.
The prince’s team will help you. The bash print function acts as a print function in the C/C++ programming language.
printf My brother %s – %d year.n Prakash 21
Can you guess the result?
The first argument %s expects a string, %d expects a decimal number and C/C++. Don’t worry if you don’t know C/C++.
You don’t have to learn this to use the print command in your bass scripts. I’ll show you some examples from the Bash printer team.
Using the Bash print function command
Let’s start with the syntax.
Print format
Here the format is a string that determines how the following values are displayed.
In the sample My Brother %s – %d years.n Prakash 21, all %s My Brother – %d years.n – format, followed by Prakash and 21 arguments. The arguments are used to replace the format specifications (%s, %d, etc.), which I will explain later in this article.
In its simplest form, print can be used as an echo command to display the string.
Prince Hello World
Did you notice the new character in the n-string at the end? The difference between echo and printf is that echo automatically adds a linefeed character at the end, but for printf it has to be added explicitly.
Pay special attention to the type and number of arguments
Don’t forget that the formatting string tries to apply to all arguments. So you should pay special attention to it.
Hello, %s! n Abhishek Prakash
Hello, Abhishek!
Hello Prakash!
You should also pay attention to the type of format it expects in the format string.
As you can see in the example above, if it doesn’t find the expected arguments, it will yield default values that are zero for strings and zero for integers.
printf Hi %s, your room number %d. n Abhishek Prakash 131
bash: printf : Prakash: Invalid number
Hello Abhishek, your number is 0
Hello 131, your room number is 0.
Here Abhishek Prakash is taken as the first group of arguments and 131 as the second group of arguments.
If it finds a string (Prakash) instead of an integer, it complains, but still prints a second argument with the default value 0.
Similarly, it considers 131 to be a string in the second group of arguments, and since the second argument is missing, the default value is set to 0.
Symbols in specification format
There are various format specifications for displaying the output in the desired format. Here are some of the most common:
Character | Use |
---|---|
%s | Cord of characters |
%c | Uniformity |
%d | Integrators |
%o | The October Whole |
%x | Hexadecimal numbers |
%f | Floating point |
%b | Line with the backslash symbol |
%% | Percentage sign |
Now that you know the format specification, we’ll show you a few more examples that show it in action.
Example Bash Team Printout
I don’t think you need a detailed explanation for most of these examples, because they are self-explanatory.
Secure email]:~$ print Eight times %d equals %d to 30 30
Eight times 30 equals 36
Consider using the %b specification to correctly interpret the shielded backslash character.
~$ Print line with backslash: %sn HellonWorld!
Cord with a backslash: Hell is the world!
See, for the %s, it doesn’t matter. But with %b the new flight character of the string is correctly interpreted:
~$ Print line with backslash: %bn HellonWorld!
Cord with a backslash: Hello, world!
If you use %c, it only reads one character at a time.
E-mail security]:~$ Printing symbol: %cn a
Symbol: a
[E-mail security]:~$ Printing symbol: %cn a b
Symbol: a
Symbol: b
Symbol: c
[E-mail security]:~$ Printing symbol: %cn abc
Symbol: a
Use of modifiers to display printed outputs in a certain style
There are modifiers to adapt the appearance of the output to your needs.
# Modifier for octal and hexadecimal numbers
You previously saw the use of %o to convert a decimal fraction into an octal fraction. However, it was not entirely clear that this was an octal number. You can use the # modifier to display octal and hexadecimal numbers in the correct format.
[secure email]:~$ printf %d is %#o in octal and %#x in hexadecimal 30 30
30 is 036 in octal and 0x1e in hexadecimal
Spatial modifier for positive integers
You can use the space between % and d in %d to display a positive integer with a headspace. It is useful to have a column of positive and negative numbers. Let’s see which one’s the best:
E-mail security]:~$ printf %d n%d n n 10 -10 10
10
-10
10
[E-mail security]:~$ printf %d n%d n%d n 10 -10 10
10
-10
10
Width converter
The width modifier is an integer that specifies the minimum width of the argument field.
By default, it is correctly aligned:
Secure email]:~$ print %10s| %5dn Age 23
Age| 23
You can align it to the left by adding a flag –
Secure email]:~$ print %-10s| %-5dn Age 23
Age | 23
Precision modifier
You can use a precision modification point (.) to specify the minimum number of digits to be displayed with %d, %u, %o, %x It adds a zero padding to the left of the value.
E-mail Security]:~$ print format: %.5dn 23 reel number
: 00023
When you use a precision modifier with a string, it indicates the maximum length of the string. If the line is longer, it will be truncated in the display.
[Secure Email]:~$ print Name: %.4sn Abhishek
Name : Abhi
You can combine the width and precision modifier:
[Secure Email]:~$ print Name: %.4sn Abhishek
Name : Abhi
[Email Protection]:~$ print Name: %10.4sn Abhishek
Name : Abhi
Bonus Example: Table output with printout– Output
Let’s put what you’ve learned about the print team into a slightly more complex example. This will show the true potential of the print team in bash-scripts.
Let’s use the print command to print the following table with bash:
Name | IDENTIFICATION CODE | Age | Classes |
---|---|---|---|
Sherlock Holmes | 0000122 | 23 | A |
James Bond | 0000007 | 27 | F |
Hercules Poirot | 0006811 | 59 | G |
Jane Marple | 1234567 | 71 | C |
Here’s the shell script I wrote for this job. Your scenario may seem different:
#/bin/bash
seperator=3d|
seperator=$seperator$seperator
rows=%-15s| %.7d| %——————– %cn
TableWidth=37.
printf %-15s| %-7s| %.3s| %sn ID Age category
printf %.${Width chart}sn $seperator
printf $lines Sherlock Holmes 122 23 A
printf $lines James Bond 7 27 F
printf $lines Hercules Poirot 6811 59 G
printf $lines Jane Marple 1234567 71 C
If I run this script, he’ll print it:
Isn’t it great to use the bash print command to print properly formatted output for your scripts?
I hope you enjoyed this detailed guide for the printf Linux team. If you have any questions or suggestions, don’t hesitate to let me know. And don’t forget to consult the Linux manual for more information about Linux.
Related Tags:
linux graphical ping,gping windows,netscantools basic,open visual traceroute,ping monitor reddit,prettyping,fping command in linux example,fping command windows,fping list of subnets,ping sweep fping,fping get hostname,fping return code,how to ping an ip address windows 10,ping ip address online,how to ping multiple ip addresses in cmd,best ping tool,ping ip address linux,how to ping an ip address on mac,ping command in linux with port,ping command in linux example,linux ping command not found,ping with packet size 1500,ping flood command windows,linux ping don't fragment