• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Echo Command in Linux with Examples

Written by Admin, Updated On November 21, 2020
bash, terminal
Echo Command in Linux with Examples

In Linux, the echo command is most common and frequently used command. It will print the passed arguments to the standard output. Generally, echo command is used in shell scripts to show a message or output the results of other commands.

echo Command#

By default, the echo is built-in in Bash and other shells like Zsh and Ksh. It reacts different in different shell.

In this guide we will cover the Bash built-in version of echo. There is also a standalone /usr/bin/echo utility, but usually, the shell built-in version will take precedence.

The basic syntax for the echo command is as following:

echo [-neE] [ARGUMENTS]
  • When the -n option is used, the trailing newline is suppressed.
  • If the -e option is given, the following backslash-escaped characters will be interpreted:
    • \\ – Shows a backslash character.
    • \a – Alert (BEL)
    • \b – Displays a backspace character.
    • \c – Suppress any further output
    • \e – It displays an escape character.
    • \f – Displays a form feed character.
    • \n – It shows a new line.
    • \r – Show a carriage return.
    • \t – Display a horizontal tab.
    • \v – It will display a vertical tab.
  • The -E option disables the interpretation of the escape characters. This is the default.

Following are points to be keep in mind while using the echo command:

  • Before passing the arguments to the echo command, the shell will substitute all variables, wildcard matching, and special characters.
  • It’s best practice to use double or single quotes to enclose the passed arguments to echo. This is mandatory to use but it’s recommended.
  • When using single quotes '' the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.

echo Examples#

The following examples show how to use the echo command:

Print a line of text on standard output.

echo Hello, TecNStuff!
Hello, TecNStuff!

Display a line of text containing a double quote.#

To print a double quote, enclose it within single quotes or escape it with the backslash character.

echo 'Hello "TecNStuff"'
echo "Hello \"TecNStuff\""
Hello "TecNStuff"

Display a line of text containing a single quote.#

You should enclose the string using double quotes use the ANSI-C Quoting, to print the single quotes

echo "I'm doing good."
echo $'I\'m doing good.'
I'm doing good.

Display a message containing special characters.#

Use the -e option to enable the interpretation of the escape characters.

echo -e "If it doesn't challenge, it wont change you.\n\t- Kunj"
If it doesn't challange, it wont change you.
    - Kunj

Pattern matching characters.#

You can use the echo command with pattern matching characters, like the wildcard characters. For instance, below command will show the names of all the .txt files in the current working directory.

echo The TXT files are: *.txt

The PHP files are: numbers.txt names.txt clac.txt

Output to a file#

You can redirect the output to the file using the >, >> operators, instead of displaying the output on the screen.

echo -e 'In a gentle way, you can shake the world. \nGandhiji' >> /tmp/file.txt

It will create a file.txt if it doesn’t exist. If you use the > operator it will be overwrite, while use the >> will append the output to the file.

Use the cat command to view the content of the file:

cat /tmp/file.txt
In a gentle way, you can shake the world. 
Gandhiji

Displaying variables#

The echo can be use to display variables. Let’s see an example to print the name of the currently logged in user:

echo $USER
tecnstuff

$USER is a shell variable that holds your username.

Displaying output of a command#

Use the $(command) expression to include the command output in the echo’s argument. The following command will display the current date :

echo "The date is: $(date +%D)"
The date is: 10/20/20

Conclusion#

You learned how to use the echo command in Linux.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Touch Command in Linux
Next Article   Bash Case Statement

Related Posts

  • How to Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Rename Files and Directories in Linux

    How to Rename Files and Directories in Linux

    December 15, 2020

Leave a Reply Cancel reply

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 20.04 February 14, 2021
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook