echo
Print text to the terminal
TLDR
Print a text message. Note: Quotes are optional
Print a message with environment variables
Print a message without the trailing newline
Append a message to the file
Enable interpretation of backslash escapes (special characters)
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)
SYNOPSIS
echo [OPTION]... [STRING]...
PARAMETERS
-n
Do not output the trailing newline character.
-e
Enable interpretation of backslash escape sequences. Common sequences include \n for a newline, \t for a horizontal tab, and \c to suppress further output.
-E
Disable interpretation of backslash escape sequences. This is often the default behavior for the /bin/echo utility, but not necessarily for shell built-ins. Use this to ensure backslashes are printed literally.
DESCRIPTION
The echo command is a fundamental Unix/Linux utility used to display lines of text or the values of variables to standard output. It's commonly employed in shell scripts for debugging, providing user feedback, or outputting data that can be piped or redirected to files. When invoked, echo prints its arguments separated by spaces, typically followed by a newline character. Different versions of echo exist: a shell built-in command (present in most shells like Bash, Zsh, Dash) and an external utility (e.g., /bin/echo). Their behaviors, particularly regarding the interpretation of backslash escape sequences, can vary, which is an important consideration for scripting portability.
CAVEATS
Portability Issues: The behavior of echo, especially concerning backslash interpretation (whether -e is implicitly enabled or not), is not consistent across all Unix-like systems and shell versions. This makes echo less portable for complex string manipulations. For robust and portable scripting, the printf command is generally preferred.
Built-in vs. External: Most shells have their own built-in echo command, which takes precedence over the /bin/echo executable. The behavior of the built-in can differ from the external command. To explicitly use the external command, you can specify its full path (e.g., /bin/echo).
BACKSLASH ESCAPE SEQUENCES (WITH -E OPTION)
When the -e option is used, echo interprets the following common backslash-escaped characters:
\a alert (bell)
\b backspace
\c produce no further output
\e escape
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\0NNN byte with octal value NNN (1 to 3 digits)
\xHH byte with hexadecimal value HH (1 to 2 digits)
HISTORY
The echo command has been a fundamental component of Unix since its earliest versions, making it one of the oldest and most widely used utilities. Its simplicity in printing text made it instantly valuable. However, the lack of a standardized behavior for backslash escape sequences in early implementations led to significant variations between different Unix systems and later, between different shell built-ins and the standalone /bin/echo utility. This inconsistency eventually prompted efforts by POSIX to standardize some of its behavior, and also led to the introduction and increasing preference for the more predictable and versatile printf command for new scripting.