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.
➊ At first, launch an Ubuntu Terminal.
➋ Now, write the following command to open a file in the nano text editor:
nano Hello.sh
➌ Afterward, write the following script in the nano editor:
#! /bin/bash
printf “Hello, world! \n”
➍ 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
- 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.
➏ Finally, run the script by the following command:
./Hello.sh
The 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.
Script (string.sh) >
#! /bin/bash
name= “Jane”
printf “Hello, %s!\n” “$name”
Now, run the script by the following command:
./string.sh
Here, 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.
Script (decimal.sh) >
#! /bin/bash
num=42
printf “The answer is %d. \n” “$num”
Now, run the script by the following command:
./decimal.sh
The 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.
Script (floating.sh) >
#! /bin/bash
pi=3.14159
printf “The value of pi is %.2f. \n” “$pi”
Now, run the script by the following command:
./floating.sh
The 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’.
Script (data.sh) >
#! /bin/bash
name= “Austen”
age=06
printf “Name: %-10s Age: %02d\n” “$name” “$age”
Now, run the script by the following command:
./data.sh
Here, 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.
Script (hexa.sh) >
#! /bin/bash
num=105
printf “Hexadecimal: %x\n” “$num”
Now, run the script by the following command:
./hexa.sh
The 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
Related Articles
- What is Echo Command in Bash [With 3 Practical Examples]
- How to Echo New Line in Bash [6 Practical Cases]
- Cat Command in Bash [With 4 Practical Examples]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Suppress Output in Bash [3 Cases With Examples]
- How to Change Color of Output Using Bash Script? [Easy Guide]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial