While displaying the text as the output of a program, you might have certain requirements regarding on how the text should appear, such as its spacing, alignment, etc. There are certain ways of achieving the desired formatting of the output in programming. In this article, we will focus more on the method of padding a string with spaces in Bash in Linux Mint 20.3.

How to Pad a String with Spaces in Bash in Linux Mint 20.3?

We designed the following script to demonstrate the method of padding a string with spaces in Bash:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1365.png" data-lazy- height="241" src="data:image/svg xml,” width=”649″>

Our goal is to generate and print five random numbers in a certain format with padded spaces. For that, we created a file in our home directory named “Space.sh” and included Shebang (#!/bin/bash) in this file. Then, we used a “for” loop to print these five numbers on the console. Within this loop, we inserted a “do-done” block. Inside this block, we used the “$Random” function of Bash to generate random numbers. We printed the generated numbers followed by five spaces and a message display “Number Generated”. Then, we used another “printf” statement to print all the five random numbers in a separate line.

To run this Bash script, we executed the following command in our terminal:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1366.png" data-lazy- height="26" src="data:image/svg xml,” width=”335″>

The output of this Bash script is shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1367.png" data-lazy- height="127" src="data:image/svg xml,” width=”424″>

To exceed the padded spaces, increase the number of spaces in the same Bash script as shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1369.png" data-lazy- height="240" src="data:image/svg xml,” width=”648″>

We want to pad our desired string with 7 spaces in this Bash script.

You can compare the outputs of both Bash scripts in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1373.png" data-lazy- height="226" src="data:image/svg xml,” width=”469″>

Similarly, if you want to pad 10 spaces to your desired string, make use of the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1375.png" data-lazy- height="244" src="data:image/svg xml,” width=”651″>

The following image displays the output of all three Bash scripts in the order of increasing padded spaces:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1378.png" data-lazy- height="330" src="data:image/svg xml,” width=”479″>

Conclusion

You can easily pad your desired strings with the required number of spaces by following the prescribed method. This way, you can properly format your text or output. More related articles are available on the website for more tips and tutorials.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/karim-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.