How To

Bash printf Command Explained With Examples

Introduction

The printf command in Bash is used to print formatted text in the terminal. It is similar to the echo command, but printf gives more control over output formatting. With printf, you can control spacing, new lines, numbers, columns, and text alignment.

If you are learning Bash scripting, the printf command is very useful. It helps you create clean output, formatted reports, tables, logs, menus, and system information scripts. For Linux automation and cybersecurity scripts, formatted output makes results easier to read and understand.

What Is The printf Command In Bash?

The printf command prints text using a format string.

Basic syntax:

printf "format" values

Example:

printf "Hello, Bash scripting\n"

Output:

Hello, Bash scripting

The \n adds a new line. Unlike echo, printf does not automatically add a new line unless you include \n.

Basic printf Example

Create a new Bash script:

nano printf-example.sh

Add the following code:

#!/bin/bashprintf "Welcome to Bash scripting\n"printf "This is printed using printf\n"

Save and run it:

chmod +x printf-example.sh./printf-example.sh

Output:

Welcome to Bash scriptingThis is printed using printf

Using Variables With printf

You can use variables inside printf.

#!/bin/bashname="Kali Linux"topic="Bash Scripting"printf "Name: %s\n" "$name"printf "Topic: %s\n" "$topic"

Here, %s is used for strings.

Output:

Name: Kali LinuxTopic: Bash Scripting

Common printf Format Specifiers

FormatMeaning
%sString
%dInteger number
%fFloating-point number
\nNew line
\tTab space

Example:

#!/bin/bashtool="Nmap"ports=1000printf "Tool: %s\n" "$tool"printf "Ports scanned: %d\n" "$ports"

Create A Simple Table Using printf

The printf command is very useful for creating aligned output.

#!/bin/bashprintf "%-15s %-10s\n" "Tool" "Use"printf "%-15s %-10s\n" "Nmap" "Scanning"printf "%-15s %-10s\n" "Curl" "Requests"printf "%-15s %-10s\n" "Grep" "Search"

Output:

Tool            UseNmap            ScanningCurl            RequestsGrep            Search

The %-15s means print a left-aligned string with 15 spaces.

Cybersecurity Example: Format Scan Result

#!/bin/bashhost="192.168.1.1"status="Online"printf "Host: %-15s Status: %s\n" "$host" "$status"

This type of formatting is useful when displaying scan results, log summaries, or system reports.

Conclusion

The Bash printf command is a powerful way to print formatted output. It gives better control than echo and is useful for creating clean terminal output, reports, tables, and logs.

For beginners, learning printf helps improve the quality of Bash scripts. It is especially useful in Linux automation, cybersecurity scripting, system monitoring, and command-line tools where readable output matters

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

3 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

3 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

4 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

7 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

7 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

7 days ago