How to Print Output in Bash [With 6 Practical Examples]

In Bash scripting, knowing how to print output accurately & efficiently is a must-have skill. Whether you need to display simple text, formatted numbers, variable values, or complex data, Bash provides several methods to meet your output needs. In this article, I will discuss basic ways to print in Bash & then I will discuss how you can print customized outputs using the printf command with some practical examples.

Different Ways to Print in Bash

There are several ways to print output in Bash. Here are some different methods:

A. “echo” Command

The echo command is a built-in command in bash scripting that is used to display text to the terminal. This is the simplest command to print any output in the bash scripting. Also, it automatically adds a new line character at the end of the output.

B. “cat” Command

The cat command is a which displays the contents of files or concatenates multiple files and displays the combined output.

C. “printf” Command

The printf command is used to format and print text in a specific manner. It allows you to control the formatting of the output, including the use of placeholders for variables. This command provides more control over the output format compared to the ‘echo’ command.

D. Here Document

A here document allows users to print multiple lines or a block of text by specifying it directly in the script. Generally, it is useful when a user wants to print formatted or multiline text.

Out of all these ways, in this writing, I will discuss how you can print output by executing Bash scripts using the printf command with some practical examples.

The “printf” Command in Bash

In Bash, ‘printf’ is a built-in shell command used to format & print text or data. It allows you to customize outputs by specifying format specifiers for various types of data and formatting options. It’s more powerful than the ‘echo’ command in terms of formatting output.

Command Syntax >

printf [FORMAT] [Argument]...

Here,

  • FORMAT → Specifies the format string, which contains format specifiers and optional formatting instructions.
  • Argument → Refers to the data or variables to be substituted into the format specifiers defined in the format string.

Format Specifiers >

  • %d → Considers the input as a decimal (integer) number.
  • %c → Considers the arguments as a single character.
  • %i → Holds an integer number as an input.
  • %s → Considers the input as a string of characters.
  • %x → Holds a hexadecimal number as an input.
  • %f → Holds a floating number as an input.
  • %% → Prints a percent sign.
  • \n → Prints a new line.
  • \t → Prints a horizontal tab.

6 Examples of the “printf” Command

In the following article, I will present you with 6 practical examples of the ‘printf’ command. Where you can see how to use format specifiers with the command arguments to customize the desired output.

Example 1: Printing a Simple String Using Bash Script

To print a simple string using the ‘printf’ command, check out the following script:

#! /bin/bash

printf "Hello, world! \n"

EXPLANATION

Here, the printf command simply prints the quoted message followed by a newline character \n.

Run a bash scriptThe output is simply printing a string ‘Hello, world!’.

Example 2: Printing a String With Variable Substitution Using Bash Script

The ‘printf’ command helps users print a simple string with the value assigned to a variable.

Review the script below to print a string with variable substitution:

#! /bin/bash

name= "Jane"
printf "Hello, %s!\n" "$name"

EXPLANATION

At first, a value “Jane” is assigned to the variable named “name”. Then, the printf command prints a message where %s is a placeholder for a string & the value of the variable $name is substituted into the placeholder.

Print a string with variable substitution in bashIn the image, the command ‘printf’ is outputting the string along with the variable ($name) value, that is Jane.

Example 3: Printing Formatted Numbers Using Bash Script

The ‘printf’ command allows users to print formatted numbers using different format specifiers. For example, the format specifier %d is used to place a decimal (integer) number as input.

Here’s a script to print formatted numbers in Bash:

#! /bin/bash

num=42
printf "The answer is %d. \n" "$num"

EXPLANATION

Here, %d is a placeholder for a decimal (integer) number. The variable ‘$num’ value is substituted into the placeholder to print the output.

Print a decimal value in bashThe output displays a decimal number 42, which was placed inside the ‘$num’ variable.

Example 4: Formatting Floating-Point Numbers Using Bash Script

The ‘printf’ command helps in formatting floating-point numbers using the format specifier %f. Here’s how to do it:

#! /bin/bash

pi=3.14159
printf "The value of pi is %.2f. \n" "$pi"

EXPLANATION

In this script, %2f is a placeholder for a floating-point number with two decimal places. The variable ‘$pi’ value is substituted into the placeholder, to display in the output.

Print a floating-point value in bashThe output is showing a floating number 3.14, with two decimal places. This value was placed inside the $pi variable.

Example 5: Specifying a Width and Alignment for a String

Navigate through the script below to specify a width and alignment for string and number placeholders in ‘printf’ command:

#! /bin/bash

name= "Austen"
age=06

printf "Name: %-10s Age: %02d\n" "$name" "$age"

EXPLANATION

Here, %-10s specifies a left-aligned string with a width of 10 characters, and %02d specifies a zero-padded decimal number with a width of 2 characters. The values of the variables ‘$name’, and ‘$age’ are substituted into the respective placeholders to print the output with alignment.

Print a formatted string in bashHere, the outputs are presented in the way they were supposed to be. The first variable value Austen is a left-aligned string that can have a width of 10 characters & the second variable value 06 is a zero-padded decimal number.

Example 6: Printing Hexadecimal Value Using Bash Script

The ‘printf’ command prints hexadecimal numbers using the format specifier %x. In this case, a decimal number is used as an input variable value & the command with its specifier substitutes the decimal value into the hexadecimal output value.

Here’s a script to print hexadecimal value in Bash:

#! /bin/bash

num=105
printf "Hexadecimal: %x\n"  "$num"

EXPLANATION

Here %x is a placeholder for a hexadecimal number & the value of the variable ‘$num’ is substituted into the placeholder to display the output.

Print a hexadecimal value in bashThe output displays 69, which is the corresponding hexadecimal value for the decimal number 105 that was placed in the $num variable.

Conclusion

In conclusion, throughout this writing, I tried to discuss the basic ways of printing in Bash. Among them, I gave a thorough description of the ‘printf’ command with some practical examples. Hope this article helps you learn how to print output in Bash.

People Also Ask

How do you print in bash?

To print in Bash, you can use either the echo or printf command. For example, you can use the ‘echo’ command followed by a text like the syntax echo "Hello, Linux!" or use the ‘printf’ command using the syntax printf "Hello, Linux! \n".

How do I print a line in bash?

To print a line in Bash, you can use the echo command followed by the text you want to print. The command will output the specified text followed by a newline character. That means it will be displayed as a separate line in the terminal.

How to use printf command in bash?

To use the printf command in Bash, follow the basic command syntax printf format_string [arguments]. Here, the format_string defines the format of the output and the arguments are the values to be printed based on the format specifiers.

How do I print a user in bash?

To print the username of the currently logged-in user in Bash, you can use the command whoami. You can store the command’s output as a variable for further use in your Bash script using username=$(whoami).

Related Articles


<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment