echo

echo

The echo command in Unix and Linux is used to display a line of text or string to the standard output (usually the terminal). It is a simple but essential command that is frequently used in shell scripts and command-line operations to print messages, variables, or results of commands.

Basic Usage

The syntax for the echo command is:

echo [option] [string...]

Examples

Displaying a Simple Text

To display a simple string of text:

echo "Hello, World!"

This will output:

Hello, World!

Displaying the Value of a Variable

To display the value of a variable, use the variable name preceded by a dollar sign ($):

NAME="Alice"
echo "Hello, $NAME!"

This will output:

Hello, Alice!

Concatenating Strings

You can concatenate multiple strings or variables:

FIRST="Alice"
LAST="Smith"
echo "Hello, $FIRST $LAST!"

This will output:

Hello, Alice Smith!

Options

The echo command supports several options that modify its behavior:

  • -n: Do not output the trailing newline.

  • -e: Enable interpretation of backslash escapes.

  • -E: Disable interpretation of backslash escapes (default).

Using the -n Option

The -n option prevents echo from adding a newline at the end of the output:

echo -n "Hello, World!"

The output will be:

Hello, World!%

(Note: The % symbol here represents the prompt appearing immediately after the output.)

Using the -e Option

The -e option allows the interpretation of backslash escapes, which can be used to format the output:

  • : New line

  • : Horizontal tab

  • \b: Backspace

  • \a: Alert (bell)

  • : Carriage return

  • \\: Backslash

  • \c: Suppress further output

For example:

echo -e "Hello,\nWorld!"

This will output:

Hello,
World!

Another example with tabs and backslashes:

echo -e "Column1\tColumn2\tColumn3"
echo -e "Data1\tData2\tData3"

This will output:

Column1 Column2 Column3
Data1   Data2   Data3

Practical Use Cases

Using echo in Scripts

In shell scripts, echo is often used to display messages or debug information:

#!/bin/bash
echo "Script starting..."
echo "Current directory is $(pwd)"
echo "Script completed."

Redirecting Output

The output of echo can be redirected to files or used as input for other commands:

  • To a File: Redirect the output to a file using > or >>:

    echo "This is a test." > output.txt  # Overwrites the file
    echo "Another line." >> output.txt  # Appends to the file
  • As Input: Use the output as input for another command using | (pipe):

    echo "This is a test." | tr 'a-z' 'A-Z'

    This will convert the text to uppercase:

    THIS IS A TEST.

Conclusion

The echo command is a fundamental and versatile tool in Unix and Linux environments, useful for displaying messages, debugging scripts, and redirecting output. Its simplicity and ease of use make it an essential part of shell scripting and command-line operations. Understanding the various options and use cases for echo can greatly enhance your productivity and effectiveness when working with the command line.

man

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 supersedes the version described here.  Please refer to your
       shell's documentation for details about the options it supports.

Last updated