How to Write Echo Command Correctly in Linux

Here is the way to write the echo command correctly in Linux. The command’s usage is to display the text. When spaces are part of the text, it behaves differently. Below, you will find some examples of how it works.

Linux echo command

Echo terminal command

In the command prompt, define a variable with some value. And print the same using the Echo command. The Echo will remove all leading, trailing, and extra spaces in between. It is how it works in the command prompt (terminal). Checkout the top 10 Linux commands you should know.

x= " a b c" 
echo $x
the output will be:
a b c
echo command
Advertisements

Echo command in the shell-script

In the shell script, you need to double-check for extra spaces. The echo command in shell-script considers those also.

Here for the x1 variable, the value has two spaces. In the output in the calculation, it takes into account extra space.

Echo in shell script

You can see the result. It displayed a total of 7 characters that included one extra space.

Shell script echo extra spaces

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.