printf command in Linux with Examples

18 Feb, 2021
“printf” command in Linux is used to display the given string, number or any other format specifier on the terminal window. It works the same way as “printf” works in programming languages like C.
Syntax:
$printf [-v var] format [arguments]

Note: printf can have format specifiers, escape sequences or ordinary characters.
Format Specifiers: The most commonly used printf specifiers are %s, %b, %d, %x and %f.
Examples:
%s specifier: It is basically a string specifier for string output.$printf "%s\n" "Hello, World!"
Output:
$printf "%s\n" "Hello, World!"

Output:
%b specifier: It is same as string specifier but it allows us to interpret escape sequences with an argument.Input:  printf "%s\n" "Hello, World! \n" "From Geeks For Geeks\n"

Output: Hello, World! \n
        From Geeks For Geeks\n
Note: The escape sequence “\n” in the above lines has no special meaning when used with “%s” specifier and is treated just like other characters. If we replace “%s” with “%b“, Then it will be interpreted as a newline character.Input :  printf "%b\n" "Hello, World! \n" "From Geeks For Geeks\n"
Output:  Hello, World! 

         From Geeks For Geeks 

Example:$printf "%b\n" "Geeks" "for" "\nGeeks\n"
Output:
Input:  printf "%s\n" "Hello, World! \n" "From Geeks For Geeks\n"

Output: Hello, World! \n
        From Geeks For Geeks\n

Note: The escape sequence “\n” in the above lines has no special meaning when used with “%s” specifier and is treated just like other characters. If we replace “%s” with “%b“, Then it will be interpreted as a newline character.
Input :  printf "%b\n" "Hello, World! \n" "From Geeks For Geeks\n"
Output:  Hello, World! 

         From Geeks For Geeks 


Example:
$printf "%b\n" "Geeks" "for" "\nGeeks\n"

Output:
%d specifier: It is an integer specifier for showing the integral values.$printf "%d\n" "213" "109"
Output:
$printf "%d\n" "213" "109"

Output:
%f specifier: It is used for output of floating point values.$printf "%f\n" "1.32" "3.14"
Output:
$printf "%f\n" "1.32" "3.14"

Output:
%x specifier: It is used for output of lowercase hexadecimal values for integers and for padding the output.$printf "%08x\n" "1024"
Output:
$printf "%08x\n" "1024"

Output:
Linux Tutorials | General Purpose Utilities | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersLinux Tutorials | General Purpose Utilities | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:003:12 / 4:09•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=g6cPFj9ptdc" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
linux-command
Linux-Shell-Commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
curl command in Linux with Examples
Tail command in Linux with examples
Conditional Statements | Shell Script
TCP Server-Client implementation in C
Docker - COPY Instruction
scp command in Linux with Examples
UDP Server-Client implementation in C
echo command in Linux with Examples
Cat command in Linux with examples