Convert decimal to hexadecimal in UNIX shell script
printf "%x\n" 34
22
Source: https://vitux.com/how-to-output-text-on-linux-shell/
Bash Scripting: How to Output and Format Text on Linux Shell
Bash scripting is quite popular is the easiest scripting language. Like any programming or scripting language, you come across printing text on the terminal. This can happen in numerous scenarios such as when you want to output the contents of a file or check the value of a variable. Programmers also debug their applications by printing the values of their variables on the console. Therefore, before we delve into bash scripting which will be another tutorial, let’s look at the different ways in which we could output text in the terminal.
Echo is the most important command that you need to know in order to output text on the terminal. As the name itself suggests, echo displays number or string on standard output in the terminal. It also has a number of options available as shown in the table below.
Options Definition
-n Do not print the trailing newline
-E Disable interpretation of back-slash escaped characters
-e Enable interpretation of backslash escapes
\a Alert
\b Backspace
\c Suppress trailing newline
\e Escape
\f Form feed
\\ backslash
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
According to the Linux documentation, the following is the syntax for echo command.
echo [option(s)][string(s)]
Now, we shall see the different ways in which we can output the text on the terminal.
Bash scripting is quite popular is the easiest scripting language. Like any programming or scripting language, you come across printing text on the terminal. This can happen in numerous scenarios such as when you want to output the contents of a file or check the value of a variable. Programmers also debug their applications by printing the values of their variables on the console. Therefore, before we delve into bash scripting which will be another tutorial, let’s look at the different ways in which we could output text in the terminal.
Echo is the most important command that you need to know in order to output text on the terminal. As the name itself suggests, echo displays number or string on standard output in the terminal. It also has a number of options available as shown in the table below.
Options | Definition |
-n | Do not print the trailing newline |
-E | Disable interpretation of back-slash escaped characters |
-e | Enable interpretation of backslash escapes |
\a | Alert |
\b | Backspace |
\c | Suppress trailing newline |
\e | Escape |
\f | Form feed |
\\ | backslash |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
According to the Linux documentation, the following is the syntax for echo command.
echo [option(s)][string(s)]
Now, we shall see the different ways in which we can output the text on the terminal.
Send Text to Standard Output
To output any string or number or text on the terminal, type the following command and press enter.
echo "Hello World"
The following output will be shown on the terminal
To output any string or number or text on the terminal, type the following command and press enter.
echo "Hello World"
The following output will be shown on the terminal
Print a Variable
Let’s declare a variable and prints its value on the terminal. Suppose x is a variable which we have initialized at 100.
x=100
Now, we will output the value of the variable on the terminal.
echo x
100 will be printed on the terminal. Likewise, you can also store a string in a variable and output it on the terminal.
Try it out and let us know if it was easy for you.
Let’s declare a variable and prints its value on the terminal. Suppose x is a variable which we have initialized at 100.
x=100
Now, we will output the value of the variable on the terminal.
echo x
100 will be printed on the terminal. Likewise, you can also store a string in a variable and output it on the terminal.
Try it out and let us know if it was easy for you.
Remove Space between Words
This is one of my favorite option of echo as it removes all the space between different words in the sentences and jumbles them up together. In this feature, we will be using two of the options as mentioned in Table 1.
echo -e "Hello \bmy \bname \bis \bjohn \bDoe"
As you can see from the above example, we are enabling the interpretation of backslash escapes as well as adding backspace. The following output was shown.
This is one of my favorite option of echo as it removes all the space between different words in the sentences and jumbles them up together. In this feature, we will be using two of the options as mentioned in Table 1.
echo -e "Hello \bmy \bname \bis \bjohn \bDoe"
As you can see from the above example, we are enabling the interpretation of backslash escapes as well as adding backspace. The following output was shown.
Output Word in New Line
This option of echo comes in really handy when you are working bash scripting. Mostly you need to move to the next line once you are done. Therefore, this is the best option to use for that.
echo -e "Hello \nmy \nname \nis \nJohn \nDoe"
The output will display each word in a separate line as shown in the screenshot below.
This option of echo comes in really handy when you are working bash scripting. Mostly you need to move to the next line once you are done. Therefore, this is the best option to use for that.
echo -e "Hello \nmy \nname \nis \nJohn \nDoe"
The output will display each word in a separate line as shown in the screenshot below.
Output Text with Sound
This is a simple option of outputting text with bell or alert. To do this, type the following command.
echo -e "Hello \amy name is John Doe"
Make sure that your system’s volume is high enough for you to hear the tiny bell that sounds when the text is outputted on the terminal.
This is a simple option of outputting text with bell or alert. To do this, type the following command.
echo -e "Hello \amy name is John Doe"
Make sure that your system’s volume is high enough for you to hear the tiny bell that sounds when the text is outputted on the terminal.
Remove Trailing New Line
Another option of the echo is to remove the trailing newline so that everything outputs on the same line. For this, we use “\c” option as shown in the figure below.
echo -e "Hello my name \cis John Doe"
The following output is shown
Another option of the echo is to remove the trailing newline so that everything outputs on the same line. For this, we use “\c” option as shown in the figure below.
echo -e "Hello my name \cis John Doe"
The following output is shown
Add a Carriage Return to the output
To add a specific carriage return in your output, we have “\r” option for this.
echo -e "Hello my name \ris John Doe"
The following output is shown to you on the terminal.
To add a specific carriage return in your output, we have “\r” option for this.
echo -e "Hello my name \ris John Doe"
The following output is shown to you on the terminal.
Use Tabs in Output
While printing output on the terminal, you can add horizontal and vertical tabs as well. These come in handy for cleaner outputs. To add horizontal tabs, you have to add “\t” and for vertical tabs, add “\v”. We will be doing a sample for each of these and then a combined one.
echo -e "Hello my name \tis John Doe"
The output for this command will be shown as follows
echo -e "Hello my name \vis John Doe"
The output for this command will be shown as follows
Now we will combine this example for a set of sentences we have.
echo -e "Hello my name \vis John Doe. Hello! My name is \tJane Doe"
The following will be printed on the terminal.
Well, that’s all the options that can be used for printing text on a terminal. This is an important feature to learn as it will help you further when you start working on bash scripting. Make sure that you implement each of the options and practice hard. Let us know if this tutorial helped you solve a problem.
While printing output on the terminal, you can add horizontal and vertical tabs as well. These come in handy for cleaner outputs. To add horizontal tabs, you have to add “\t” and for vertical tabs, add “\v”. We will be doing a sample for each of these and then a combined one.
echo -e "Hello my name \tis John Doe"
The output for this command will be shown as follows
echo -e "Hello my name \vis John Doe"
The output for this command will be shown as follows
Now we will combine this example for a set of sentences we have.
echo -e "Hello my name \vis John Doe. Hello! My name is \tJane Doe"
The following will be printed on the terminal.
Well, that’s all the options that can be used for printing text on a terminal. This is an important feature to learn as it will help you further when you start working on bash scripting. Make sure that you implement each of the options and practice hard. Let us know if this tutorial helped you solve a problem.