Bash Echo Examples

29/12/2020
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 "Perltistatpowerfultandtportabletlanguage"

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 "Inlikenbashprogramming"

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 "Linuxhintv 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 Linuxhintctutorials"

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 languager"
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!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash text and background printing in different colors

A terminal is a very important application for any Linux operating system. It is mainly used to execute different commands...
29/12/2020

Ansible Archive and Unarchive

Ansible is a great tool to automate your configuration management. The benefit of Ansible is that you don’t need to set...
29/12/2020

How to run multiple commands in Linux

Different types of commands are needed to run from the terminal in Linux. Sometimes we need to run multiple commands at...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024