LinuxCommandLibrary

echo

Print text to the terminal

TLDR

Print a text message. Note: Quotes are optional

$ echo "[Hello World]"
copy

Print a message with environment variables
$ echo "[My path is $PATH]"
copy

Print a message without the trailing newline
$ echo -n "[Hello World]"
copy

Append a message to the file
$ echo "[Hello World]" >> [file.txt]
copy

Enable interpretation of backslash escapes (special characters)
$ echo -e "[Column 1\tColumn 2]"
copy

Print the exit status of the last executed command (Note: In Windows Command Prompt and PowerShell the equivalent commands are echo %errorlevel% and $lastexitcode respectively)
$ echo $?
copy

Pass text to another program through stdin
$ echo "[Hello World]" | [program]
copy

SYNOPSIS

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

PARAMETERS

-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

DESCRIPTION

The echo command is a basic shell built-in or standalone utility in Unix-like systems, primarily used to output strings to standard output (stdout). It concatenates its arguments, separated by single spaces, and appends a trailing newline by default.

Essential for shell scripting, debugging, and simple text generation, echo supports options to control newline output and interpret backslash escape sequences for special characters like newlines (\n), tabs (\t), and more.

Example: echo "Hello, World!" prints Hello, World! followed by a newline. Use echo -n "No newline" to suppress it. With -e, escapes are processed: echo -e "Line1\nLine2" outputs two lines.

While ubiquitous, echo's full feature set (escapes, options) is GNU-specific. POSIX mandates only basic echoing without trailing newline if ending with \c, but implementations vary (e.g., dash echo ignores most options). Prefer printf for portability and precision.

CAVEATS

Behavior varies by shell and implementation (e.g., bash vs. dash echo ignores -e). Not fully portable; escape handling and options are GNU extensions. Always quote arguments to preserve spaces/special chars. Avoid for complex formatting—use printf.

ESCAPE SEQUENCES (-E ENABLED)

\a (bell), \b (backspace), \c (suppress newline), \e (escape), \f (form feed), \n (newline), \r (carriage return), \t (tab), \v (vertical tab), \\ (backslash), \0nnn (octal), \xHH (hex)

SHELL BUILT-IN VS. EXTERNAL

Often a shell built-in (faster, overrides /bin/echo). Use command echo or full path for external. Built-ins may lack full options.

HISTORY

Originated in Version 6 Unix (1975), standardized in POSIX.1-2001 (basic form). GNU coreutils version since 1990s adds escapes/options for scripting. Remains a shell staple despite printf superiority.

SEE ALSO

printf(1), cat(1), tee(1)

Copied to clipboard