Skip to content

How to Use echo Command

homepage-banner

What Is the Echo Command?

The echo command allows you to display text or strings in the terminal that are passed to it as arguments.

If you assign a value to a variable and want to know what that value is, you can use the echo command to display it. As the name suggests, the command echoes the text or string you want to display. You can also use the echo command to display files or directories, write to a file, or use it in combination with other commands.

Use Echo command to pass arguments

The following sections provide examples of the most frequently used options with the command:

  • e: display text in different formats
  • *: display files or directories
  • n: remove the trailing new line when displaying the arguments

Display the text or strings

To display text or a string you can use the echo command with the following options:

  • \a: Sound alert. This generates the default alert sound.

    echo -e "\aVPS"
    

    The output is (with a sound alert):

    VPS
    
  • \b: Writes a backspace character.

    echo -e "This is a\bVPS Server"
    

    The output is:

    This is VPS Server
    
  • \c: Abandons any further output.

    echo -e "This is a VPS \c Server"
    

    The output is similar to:

     This is a VPS [user@vps /]$
    
  • \e: Writes an escape character.

    echo -e "This is a dollar $\eSign"
    

    The output is:

    This is a dollar $ign
    
  • \f: Writes a form feed character.

    echo -e "The next page is: \f page2"
    

    The output is:

    The next page is:
               page2
    
  • \n: Writes a new line.

    echo -e "This is a \nVPS Server"
    

    The output is:

    This is a
    VPS Server
    
  • \r: Writes a carriage return. In other words, it overwrites the value that is printed.

    echo -e "Server \r VPS"
    

    The output is:

    VPS
    
  • \t: Writes a horizontal tab.

    echo -e "This is a \tVPS Server"
    

    The output is:

    This is a   VPS Server
    
  • \v: Writes a vertical tab.

    echo -e "This is a \vVPS Server"
    

    The output is:

    This is a
              VPS Server
    
  • \: Writes a backslash character.

    echo -e This is a backslash \\
    

    The output is:

    This is a backslash \
    

Remove the trailing space

To remove the trailing space you can use the following command:

  echo -n "remove trailing space"

The output is similar to:

  remove trailing space[user@vps /]$

List the files or directories

To view all the files and folders you can use the echo command as follows:

    echo *

The output is similar to:

  bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv swapdir sys tmp usr var

You can also use the echo command to list the files and directories that begin with b by using echo b*. To list the files of a particular format, use echo *.txt .

Writing to files

You can also use the echo command to write content to the files.

For example, to write some content to a log.txt file, you can use the following command:

  echo "Logfile started: $(date +'%D %T')" > log.txt

To view the content of the file:

  cat log.txt

The output is:

  Logfile started: 03/01/21 20:17:59

Other commands with echo

You can also echo commands with other commands.

Example:

  1. Export a function called name:

    name() { echo "VPS"; }
    
    export -f name
    

    To verify that the function is exported type:

    name
    

    The output is:

    VPS
    
  2. Change the value of the function name:

    name() { echo "VPS_Test"; }
    

    To verify that the value is passed:

    name
    

    The output is:

    VPS_Test
    
Leave your message