HxHippy

echo

Display a line of text

Overview

Displays a line of text or string that is passed as an argument. It is commonly used in shell scripts and batch files to output status text to the screen or a file, and for variable expansion.

Syntax

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

Common Options

-n

Do not output the trailing newline character.

-e

Enable interpretation of backslash escapes (behavior can vary between shell built-in and coreutils echo).

-E

Explicitly disable interpretation of backslash escapes (often default).

Backslash Escapes (with -e)

Common escapes: \ (backslash), a (alert),  (backspace), c (suppress further output), e (escape char), (form feed), (new line), (carriage return), (horizontal tab), (vertical tab), \0NNN (octal byte e.g. \077), \xHH (hex byte e.g. \x3F).

Examples

$ echo "Hello, World!"

Displays "Hello, World!" followed by a newline.

$ echo -n "No newline"

Displays "No newline" without a trailing newline.

$ echo -e "Line 1 Line 2 Indented"

Displays text on two lines with the second line indented (if -e is supported and interpreted).

$ name="User"; echo "Welcome, $name!"

Displays "Welcome, User!" by expanding the shell variable.

$ echo "Saving data..." > log.txt

Writes the string "Saving data..." to the file `log.txt`, overwriting existing content.

$ echo "Error encountered." >> error_log.txt

Appends the string "Error encountered." to `error_log.txt`.

$ echo "An important message" >&2

Redirects the message to standard error (stderr).

$ echo -e "This is red text"

Displays "This is red text" in red color using ANSI escape codes (terminal dependent).

echoprintdisplayoutputtextstringshellscript