Updated April 10, 2023
Introduction to Echo in Shell Scripting
Echo is a command in shell scripting which is a built-in command used to display a variable, result of an expression, string, number, text, any useful information in the programming, etc. can be displayed using echo command in shell scripting. It is available in most of the programming languages and most commonly used in shell scripting where bash and c shells are mostly used. This command can also be used to display the commands or arguments sent to a shell program also. We can do so many things with echo commands like displaying special characters, newlines, etc.
Syntax:
echo [option] [string]
In the above syntax of echo, option and string which are in square brackets are optional arguments and it’s not compulsory to provide those to the echo command. The options statement in square bracket is used to provide a list of options by which we can do more with echo command whereas the string is a sequence of characters (like numbers, characters, symbols, special characters) or words, etc.
When we use echo command without any options or strings then echo command will display a new line as we are pressing enter command after echo, so it will go to the new line. If we execute an echo command followed by a sequence of words then all the words will be displayed in the same order as we entered.
How does Echo work in Shell Scripting?
The echo command is used to display a variable, text, string, number, or a list of words followed by the echo command, arguments passed to the shell script so all things will be displayed on the console. The echo command is also used to display a variable to which we already assigned some value. If we didn’t provide any options or strings to the echo command then it will display a new line and in the same way, if we have list of words followed by an echo command and then enter it will display all the words in the same sequence and there is no need to provide the list of words using single or double quotes it won’t display on the screen.
When we want to display a variable using echo command then we need to enter the echo command followed by a $variable name with any spaces or whitespaces between the $ and variable name so that shell will come to know that it needs to replace with the actual value stored in the variable. We can use the echo command to display the environment variables on the system using $ preceding the environment variable names and some of the examples are as below:
x = 5
echo $x
echo $PATH (here PATH is an environment variable on any system)
We can provide options to the echo command such as –e which will make echo to interpret the additional instances of q newline, special characters, etc. so that it will consider extra newline or tab space or special characters and format the string and displays on the console after the changes. We can also print all files which are having .txt extension using the special character * preceded by the extension so that echo will list all the files with given extension in the present working directory. Example is echo “list .txt file” *.txt
Examples of Echo in Shell Scripting
So far we have seen what is an echo command in shell scripting, its syntax, and how it will work in shell scripting. Now let’s have a look at how to use echo command in different scenarios using examples and explanation respectively.
Example #1
To display a text or string on the console
Code:
echo hello, this is world
In the above example we are displaying a text “hello, this is world” on the standard output or terminal or console using the echo command followed by the text or string like the above example.
Output:
Example #2
To display a variable value on the console
Code:
x = 20
echo $x
In the above example, we have declared a variable and assigned a value 20 to it. Now we want to display the variable value on the console by using echo command followed by $ and variable name, it will display 20 on the console or standard output.
Output:
Example #3
To remove spaces from a given string and display on the console
Code:
echo -e "This \bis \bnew \bworld"
In the above example, we are using options –e which will interpret the special characters such as \b which is a backslash followed by backspace to remove all the spaces in the given string and echo command displays all words side by side without any space.
Output:
Example #4
To display a string separated by a vertical tab space on the console
Code:
echo –e "\vThis \vis \vnew \world"
In the above example, again we used options and special character v preceded by backslash which will tell the shell to interpret using the Options command to display the given string separated by a vertical tab space on the console.
Output:
Example #5
To print all files or folders in the current directory
Code:
echo *
In the above example, we are trying to display all file, folders, and any other type of content present in the current directory where * is a special character which will get all details of current directory and displays on the console.
Output:
We have so many parameters to use along with options to achieve different things such as -n used if we don’t want to print trailing newline, \r is a carriage return which makes to discard the sentence before the carriage return, \t is a horizontal tab which enables formatting of the given string a tab space horizontally, etc.
Recommended Articles
We hope that this EDUCBA information on “Echo in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.