WebMasterCampus
WEB DEVELOPER Resources

Linux echo Command

Learn Linux echo Command with examples


Linux echo Command

In Linux we can use “echo” command to display a line of text.

“echo” command mostly used in shell scripts and batch files to output status text to the screen or a file.

echo command Syntax

>> echo [option] [string]

echo command Example

>> echo "Welcome to Webmastercampus.com"

Welcome to Webmastercampus.com

echo -e command

echo -e enables the interpretation of backslash escapes code.

If -e is in effect, the following sequences are recognized:

Option Description
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\e escape
\f form feed
\ n new line
\r carriage return
\t horizontal tab
\v vertical tab

echo -e Command with \\

\\ it adds a backslash in content.

>> echo -e "Welcome to \\Webmastercampus.com\\"

Welcome to \Webmastercampus.com\

echo -e Command with \a

\a alert gives sound alert where it applies.

>> echo -e "\a Welcome to Webmastercampus.com"

 Welcome to Webmastercampus.com

echo -e Command with \b

It just back space one time.

>> echo -e "Welcome\bto Webmastercampus.com"

Welcomto Webmastercampus.com

please note e from Welcome removed after applying \b.

echo -e Command with \c

\c is not printed and omitted trailing new line.

>> echo -e "Welcome \c to \nWebmastercampus.com"

Welcome

echo -e Command with \e

\e omitted the available next character.

>> echo -e "Welcome \e to Webmastercampus.com"

Welcome o Webmastercampus.com

echo -e Command with \n

\n it give a line break or add a new line.

>> echo -e "Welcome \n to \nWebmastercampus.com"

Welcome 
 to
Webmastercampus.com

echo -e Command with \r

\r carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.

>> echo -e "Welcome to \r Webmastercampus.com"

 Webmastercampus.com

echo -e Command with \t

\t this option is used to create horizontal tab spaces.

>> echo -e " \t Welcome to \t Webmastercampus.com"

        Welcome to      Webmastercampus.com

echo -e Command with \v

\v this option is used to create vertical tab spaces.

>> echo -e "Welcome\v to\v Webmastercampus.com"

Welcome
        to
           Webmastercampus.com

echo * (ls command alternative)

echo * display all files and directories in a row adjacent to each other.

>> echo *

cities1.txt cities2.txt email_list_1 email_list_soft_link file_copy.txt fruits fruits.txt fruits2.txt list1.txt list2.txt logs music.txt music2.txt music3.txt output.txt results squash test test1 test2 test3 test4 today.txt.gz webmastercampus

echo *.txt

echo * display all txt files.

>> echo *

cities1.txt cities2.txt file_copy.txt fruits.txt fruits2.txt list1.txt list2.txt music.txt music2.txt music3.txt output.txt

echo Command output to a File

The echo can be used with a redirect operator to output to a file.

>> echo "Learning Linux Command with webmastercampus.com" > myLinuxPractice.txt 

>> cat cat myLinuxPractice.txt

Learning Linux Command with webmastercampus.com

echo -n

echo -n this option is used to omit echoing trailing newline.

>> echo -n "Welcome to webmastercampus.com"

echo single quotes

If you want to echo single quotes, you can to.

>> echo "welcome 'to' webmastercampus.com $SHELL"

echo double quotes

If you want to to echo double quote, you can do.

>> echo "welcome \"to\" webmastercampus.com $SHELL" 

Declare a variable and echo its value

>>  a = 100
>>  echo The value of new pen is $a

echo Command in Linux (Documentation)

~$ man echo

NAME
       echo - display a line of text

SYNOPSIS
       echo [SHORT-OPTION]... [STRING]...
       echo LONG-OPTION

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

       -E     disable interpretation of backslash escapes (default)

       --help display this help and exit

       --version
              output version information and exit

       If -e is in effect, the following sequences are recognized:
      
       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \e     escape

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \0NNN  byte with octal value NNN (1 to 3 digits)

       \xHH   byte with hexadecimal value HH (1 to 2 digits)

       NOTE:  your shell may have its own version of echo, which usually su‐
       persedes the version described here.  Please refer  to  your  shell's
        documentation for details about the options it supports.
Created with love and passion.