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.

Key Takeaways

  • Learning basics about different ways to print in Bash.
  • Learning about the Bash built-in command ‘printf’.
  • Practicing with some practical examples of the ‘printf’.

Free Downloads

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. Learn more about the echo command from ‘What is Echo Command in Bash‘.

B. cat command

The ‘cat’ command is a which displays the contents of files or concatenates multiple files and displays the combined output. Go through the cat command in Bash for more details.

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 you to print multiple lines or a block of text by specifying it directly in the script. It is useful when you want 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

In this first example, I will simply print a string using the ‘printf’ command. For that, I will write a simple bash script & then make it executable to run. Go through the below steps to see the whole process.

Steps to Follow >

➊ At first, launch an Ubuntu Terminal.

➋ Now, write the following command to open a file in the nano text editor:

nano Hello.sh

open a bash script file in the nano editor➌ Afterward, write the following script in the nano editor:

#! /bin/bash

printf “Hello, world! \n”

EXPLANATION
Here, #! /bin/bash: ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Next, the ‘printf’ command simply prints the quoted message followed by a newline character (‘\n’).
write inside a bash script

➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.

➎ After that, use the following command to make the script executable:

chmod u+x Hello.sh

explanation
  • chmod: Changes the permission of files and directories.
  • u+x: Argument with chmod command to add the executable permission for the user.
  • sh: File which you want to make executable.

Add execute permission to a bash script

➏ Finally, run the script by the following command:

./Hello.sh

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

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

In this example, I will use the ‘printf’ command to print a simple string with the value assigned to a variable.

You can follow the steps of Example 01, to save & make the script executable.

Script (string.sh) >

#! /bin/bash

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

EXPLANATION
At first, assigns a value “Jane” to the variable named “name”. In the next line, the ‘printf’ command is used to print a message, where ‘%s’ is a placeholder for a string & the value of the variable ‘$name’ is substituted into the placeholder.

Now, run the script by the following command:

./string.sh

Print a string with variable substitution in bashHere, you can see from the output 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

Here, ‘printf’ is used to print formatted numbers. The format specifier ‘%d’ is used to place a decimal (integer) number as input.

You can follow the steps of Example 01, to save & make the script executable.

Script (decimal.sh) >

#! /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.

Now, run the script by the following command:

./decimal.sh

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

This example demonstrates how to format floating-point numbers using the command. For that, the format specifier ‘%f’ is used.

You can follow the steps of Example 01, to save & make the script executable.

Script (floating.sh) >

#! /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.

Now, run the script by the following command:

./floating.sh

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

This example showcases how to specify a width and alignment for string and number placeholders in ‘printf’.

You can follow the steps of Example 01, to save & make the script executable.

Script (data.sh) >

#! /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.

Now, run the script by the following command:

./data.sh

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

In this example, I will print a hexadecimal number using the ‘printf’ command with the command format specifier ‘%x’. A decimal number is used as an input variable value & the command with its specifier substitutes the decimal value into the hexadecimal output value.

You can follow the steps of Example 01, to save & make the script executable.

Script (hexa.sh) >

#! /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.

Now, run the script by the following command:

./hexa.sh

Print a hexadecimal value in bashThe output displays a hexadecimal number 69, which is the corresponding hexadecimal value for the decimal number 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?
The bash ‘printf’ command is normally used for printing formatted output. Moreover, you can also use the ‘echo’ command to display any of the output simply.

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?
The ‘printf’ command in bash allows you to format & print the output texts. Here is the basic command syntax, “printf format_string [arguments]”. Here, the ‘format_string’ is used to define 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. Like this, ‘username=$(whoami)’.

Related Articles


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

Rate this post
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