Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Echo Command in Linux with Examples - DesignLinux

Oct 21 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Setup and Manage Log Rotation Using Logrotate in Linux

Prev Post

How to Setup FTP Server with Vsftpd on Raspberry Pi

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,211
  • 616,900

DesignLinux.com © All rights reserved

Go to mobile version