BASH Programming

Bash Echo Examples

Multiple built-in functions exist in bash to print the output into the terminal. ‘echo’ is one of the most used commands to print text or string data into the terminal or another command as input or a file. This command has some options that can be used with this command for special purposes. The uses of ‘echo’ command are described in this tutorial by using different examples.

Syntax:

echo [ OPTION(S) ] [ STRING ]

Options:

Option Description
-n Omit newline from the output.
-e Enable the function of backslash(/) character.
-E Disable the function of backslash(/) character.
–version Display the version information
–help Display help messages related to the uses of this command

Example-1: Using the `echo` command without any option

`echo` command can be used without any option. `echo` command of the following script is used to print a simple text,  ‘Learn bash programming from LinuxHint.com’.

$ echo "Learn bash programming from LinuxHint.com"

The following output will appear after running the script.

Example-2: Using `echo` command with -n option

‘echo’ command is used with ‘-n’ option in the following script. The new line is omitted from the output for this option.

$ echo -n "Python is an interpreted high-level programming language"

The following output will appear after running the script.

Example-3: Using `echo` command with -e option

‘echo’ command is used with ‘-e’ option in the following script. For this, the function of backslash(\) is enabled and the output is generated by adding ‘tab’ space where ‘\t’ is used in the string.

$ echo -e "Perl\tis\ta\tpowerful\tand\tportable\tlanguage"

The following output will appear after running the script.

Example-4: Using `echo` command with -E option

‘echo’ command is used with ‘-E’ option in the following script. This option disables the function of backslash(/). The new line(\n) used in the text will not work for the following command.

$ echo -E "I\nlike\nbash\programming"

The following output will appear after running the script.

Example-5: Using variable in `echo` command

The value of the variable can be printed with other string in the `echo` command by placing the variable in the text. $price variable is used in the following `echo` command. But one thing you should remember when using the variable in the echo command, that is you must enclose the variable within double quotation(“) to read the value of the variable by `echo` command. If single quotation(‘) is used in the echo command then the value of the variable will not be parsed and the variable name will be printed as output.

$ price="\$100"
$ echo 'The price of this book is $price'
$ echo "The price of this book is $price"

The following output will appear after running the script.

Example-6: Using ‘\v’ in `echo` command  

‘\v’ is used to print the output vertically. The following `echo` command will print each word of the text, “Linuxhint is a Linux based blog site” vertically.

$ echo -e "Linuxhint\v is \v a \v linux \v based \v blog \v site."

The following output will appear after running the script.

Example-7: Using ‘\c’ in `echo` command

‘\c’ is used to omit any part of the text. The following echo command will print, “Enrich your Linux knowledge from Linuxhint” by omitting the part tutorials and newline.

$ echo -e "Enrich your Linux knowledge from Linuxhint\ctutorials"

The following output will appear after running the script.

Example-8: Print the names of all files and folders using `echo` command

`echo` command can be used to read the files and folders of the current directory. When this command executes with ‘*’ then it will print the list of files and folders of the current working directory.

$ echo *

The following output will appear after running the script.

Example-9: Print the names of specific files using `echo` command

The specific file list of the current directory can be printed by using `echo` command. The following command will print the list of all text files from the current directory. In this way, you can search any file or folder by using `echo` command.

$ echo *.txt

The following output will appear after running the script.

Example-10: Using `echo` command in the bash script

This example shows the use of `echo` command in a bash script. Create a file named ‘echoexpl.sh’ and add the following script. The function of ‘\r’ is to add a carriage return in the string. In this example, ‘\r’ is used at the starting and ending of the string. Here, the ‘-e’ option is used with the `echo` command that enables the function of ‘\r’.

echoexpl.sh

#!/bin/bash
string="\rPerl is a cross-platform, open-source programming language\r"
echo -e "$string"

Run the script.

$ bash echoexpl.sh

The text value of $string variable will be printed with a newline after running the script.

Conclusion:

The result of any script can be found by printing the appropriate output. So, print options are very important for any programming language. The use of one print option in bash is discussed in this tutorial. But the same task can be performed by another option in bash and the command is ‘printf’. Hope, the reader will get knowledge about the multiple uses of `echo` command after practicing the examples of this tutorial and they will be able to apply this command properly.

For more information watch the video!

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.