- Like a C language, in UNIX “ printf ” is used to print the statement.
- “ printf “ command is an alternative of “echo” command.
Print statement: To print any statement write “printf” command in terminal and the in the double quote ” ” write a sentence to be print. Whatever you will write in double quote will be print on the terminal.
For example: in below command, we try to print “Enter any number=” on the terminal.
1 2 | $ printf “ Enter any number = ” [press enter] Enter any number = $_ [output ] |
Escape Sequence: “printf” support all escape sequence used by “ echo ” command.
Example: “\n” escape sequence is used for the new line. Means \n direct to place the cursor at the new line as shown in below command.
1 2 | $ printf “ Enter any number = \n” [press enter] Enter any number = [output ] |
Formatted String: like a C language, Unix “ printf “ command also support formatted strings.
Like a C language, %s is used for printing string.
1 2 3 | $ x=aditya [press enter & set value of variable “aditya”] $ printf “ value of x = \n” $x [press enter] Value of x = [output , value of x not print] |
Note: Value of variable x will not print because formatted string %s is not used in above command. To print value of x write command as:
1 2 | $ printf “ value of x = %s \n” $x [press enter] Value of x = aditya [output , value of x=aditya ] |
Print integer variable value using formatted string:
1 2 3 | $ x=5 [press enter & set value of variable x is “5”] $ printf “ value of x = \n” $x [press enter] Value of x = [output , value of x not print] |
Note: Value of variable x will not print because formatted string %d is not used in above command.
To print value of x write command as:
1 2 3 | $ printf “ value of x = %d \n” $x [press enter] Value of x = 5 [output , value of x =5] $_ |