Linux and Unix printf command tutorial with examples

  • 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.

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.

Formatted String: like a C language, Unix “ printf “ command also support formatted strings.
Like a C language, %s is used for printing string.

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:

Print integer variable value using formatted string:

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: