Difference Between echo and echo -eโ
Some files are private or not ready yet, if a link gives you a 404 that's why :)
This is a work in progress, new notes will be added daily
List of note Indexes
-
๐ PROJECTS -
๐ ART -
๐ TECHNOLOGY -
๐ SCIENCE -
๐ PHILO-PSYCHO -
๐ HISTORY -
๐ HEALTH -
๐ GAMING -
๐ KNOWLEDGE
.... |\| .|||. |\/| .|\\. .|.
.\\||/|. .\|..|| .\|..|||...||.....
.| .....||.|\\\\\\ ... |\\\/. .\\. .||
.\\| .|\\|..|\\ .\\\\ .\\|. .\\\\\| |\| |\\|
|\\ |\\|.. /\\\. |\|\\ \\\\|. .|\\. .\\\\|
.\\. |\\\\\\\|.. .\\| .\\||\\/|. |\\| .|/\|
\. .\\\||/\|.... \\| |\/ |\\\ |\\\| .\\| \\| ....
|\/ |\\| |\\\\\\\| |\|.\\\ \\\\. |\\\\| |\\ \\. ||..
..|||\| .. .\\\\. |\\\\\\ .\\|....|\\\. |\\\\\\ |/. |/
\\\|.. .\\\ |\\. .\\\| \\| |||\\\. ...|\\/||. .\\\
|\| |\/. /\\\\\| .\\\\. \\. .../|. .|\\\|. |\\\| |\\
. |||\\|. ..||| .\\|. |\| \\|..../\\| |\\| .\\\\\ ..
\\| .||\|.|.. |\\\|./\ \\||\. ||... .\\\\/. |\/||
..|/||. |\\\\\|. .|/\| ||. .||\\\ .|||. |\
.\/| \\\. .|\\\|||\\\||.. .|\\\\\\\|\\\\. /\\\\||. |
.|| |\\\\| .......|\\\\\\. .\\\. ..|||.... .\\\\|.. ...
\\||....|\\\\\\\\||\\\\\\\/ |\|....|||\\\\\\\/....|\\\|
||.|\||\\| .....|\\\\..\\| .|\\\. ||........ .|\|...
. |\ /\\/\\\\\\. |\. .\\\\\\. ./\\\\/ |\\\||.
.|\.|\| .|\|... .. ./\\|..|\\| ..
.|\\| .\\\\.\\\\\\\ \||...\\\|..|\\\|.||
|\. ..||....|| ... ..||\\\. .|.
... .......
โ ๐ Bash
Difference Between echo and echo -e
The main difference between echo and echo -e is how they handle backslash escape sequences in the output.
Standard echo (without -e)
-
Behavior: Treats backslashes and special characters literally
-
Example:
echo "Hello\nWorld"bashOutput:
Hello\nWorldtext
echo -e (with escape interpretation)
-
Behavior: Interprets backslash escape sequences
-
Example:
echo -e "Hello\nWorld"bashOutput:
Hello Worldtext
Common Escape Sequences
Here are escape sequences that echo -e interprets:
| Sequence | Meaning | Example |
|---|---|---|
\n | New line | echo -e "Line1\nLine2" |
\t | Tab | echo -e "Name\tAge" |
\\ | Backslash | echo -e "Path\\to\\file" |
\b | Backspace | echo -e "Hello\bWorld" |
\r | Carriage return | echo -e "Overwrite\rNew" |
\a | Alert (bell) | echo -e "\a" |
\e | Escape character | echo -e "\e[31mRed\e[0m" |
Portability Considerations
-
The
-eoption is not POSIX compliant -
For portable scripts, use
printfinstead:printf "Hello\nWorld\n"bash -
Different shells (bash, dash, zsh) may handle
echodifferently
Practical Examples
-
Creating formatted output:
echo -e "Column1\tColumn2\tColumn3"bash -
Multi-line messages:
echo -e "First line\nSecond line\nThird line"bash -
Colored output:
echo -e "\e[31mError:\e[0m File not found"bash -
Progress indicators:
echo -e "Processing...\rDone! "bash
Best Practices
- For simple output without escapes, use plain
echo - For complex formatting with escapes, use either:
echo -e(bash-specific)printf(more portable)
- When writing scripts for multiple systems, prefer
printf