Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»Bash Printf Command

    Bash Printf Command

    RahulBy RahulDecember 15, 20212 Mins ReadUpdated:December 23, 2021

    Whenever we work with bash, we use the echo command to print a standard output as it is easy to use and fits perfectly in our needs. But with the simplicity of echo, a few limitations are also attached like output formation with echo is not easy. So Printf is an effective alternative to echo. It can be easily used in bash just like we use it in other programming languages.

    The syntax of printf command is:

    printf [-v var] format [arguments]
    

    Printf can have escape sequences, format specifiers, arguments, or ordinary characters. For example:

    printf "hello world" 
    
    Output
    hello world

    Bash Printf Command Examples

    1. The printf command accepts multiple numbers of arguments and if there are more arguments than format specifiers then the format string is reused for all the arguments. For example:
      printf "%s\n" "Hello” “in” “bash” 
      
      Output
      Hello in bash
    2. Another example of printf command is:
      printf "Open points: %s\nClosed points: %s\n" “12" "21" 
      
      Output
      Open points: 12 Closed points: 65

      Here Open Points: %s\nClosed points: %s\n is a format while 12 and 21 are arguments. So there are two newline characters (\n) and two format specifiers (%s) that are replaced with the arguments while executing the command.

    Printf Escape Characters

    Some of the common escape characters for Printf are:

    • \n – Displays a new line.
    • \r – Displays a carriage return.
    • \v – Displays a vertical tab.
    • \\ – Displays a backslash character.
    • \b – Displays a backspace character.
    • \t – Displays a horizontal tab.

    Conclusion

    In this tutorial, you have learned the Linux printf command with examples.

    bash command printf
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleBest Open Source Video Players for Linux
    Next Article 10 Best Linux FTP Clients in 2022

    Related Posts

    Running Multiple Commands At Once in Linux

    Updated:August 6, 20223 Mins Read

    How to Correctly Set the $PATH variable in Bash

    2 Mins Read

    How to run a command on bash script exits

    1 Min Read

    Convert String to Lowercase in Bash – Easier Than You Think

    Updated:August 1, 20221 Min Read

    How to run a command or function on error in Bash Script

    Updated:July 30, 20223 Mins Read

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    • What are the Access Modifiers in Java
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.