echo - display a line of text.
echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION
echo is a fundamental command found in most operating systems that offer a command line. It is frequently used in scripts, batch files, and as part of individual commands; anywhere you may need to insert text. Many command shells such as bash, ksh and csh implement echo as a built-in command.
Tag | Description |
---|---|
-n | Do not output a trailing newline. |
-e | Enable interpretation of backslash escape sequences (see below for a list of these). |
-E | Disable interpretation of backslash escape sequences (this is the default). |
--help | Display a help message and exit. |
--version | Output version information and exit. |
\\ | A literal backslash character ("\"). |
\a | An alert (The BELL character). |
\b | Backspace |
\c | Produce no further output after this. |
\e | The escape character; equivalent to pressing the escape key. |
\f | A form feed. |
\n | A newline. |
\r | A carriage return |
\t | A horizontal tab. |
\v | A vertical tab. |
\0NNN | byte with octal value NNN (which can be 1 to 3 digits). |
\xHH | byte with hexadecimal value HH (which can be either 1 or 2 digits) |
Example-1:
To print string "Hello, World!" on console
$ echo "Hello, World!"
output:
Hello, World!
Example-2:
To print value of x, where x=10.
$ echo $x
output:
10
Example-3:
Use option ‘\b‘ – backspace with backslash interpretor ‘-e‘ removes all the spaces in between.
$ echo -e 'Here \bthe \bspaces \bare \bbackspaced.'
output:
Herethespacesarebackspaced.
Example-4:
Use option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.
$ echo -e 'Here \nthe \nspaces \nare \nnewlined.'
output:
Here
the
spaces
are
newlined.
Example-5:
Use option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab spaces.
$ echo -e 'Here \tthe \tspaces \thave \thorizontal \ttab \tspaces.'
output:
Here the spaces have horizontal tab spaces.
Example-6:
Use option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab spaces.
$ echo -e 'Here \vthe \vspaces \vhave \vvertical \vtab \vspaces.'
output:
Here
the
spaces
have
vertical
tab
spaces.
Example-7:
Use option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output
$ echo -e 'Here the \rcontent before this is removed.'
output:
content before this is removed.
Example-8:
Use option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.
$ echo -e "Here the content after "," is not printed , \c This is not printed on console"
output:
Here the content after , is not printed ,
Example-9:
To print all the files/folder using echo command (ls command alternative).
ubuntu@ubuntu:/usr$ echo *
output:
bin games include lib local sbin share src
Example-10:
To echo output to a file and not standard output. in below example output is redirected to test file.
$ echo "Hello World!" > test
output:
$ cat test
Hello World!