What is echo command?
The echo command in Linux is used to display text string that is passed to it as an argument. This is a built in command that is mostly used in automation shell scripts to output debug statements or status of some execution, progress to the screen.
Syntax :
echo [option] [string]
Syntax :
echo [string]
For Example: Below command, outputs the string passed to echo command to terminal.
$ echo "Linux Commands"
Linux Commands
If you want to write this same string to some file, then we can use redirects to write the output of echo command to file as,
$ echo "Linux Commands" > helloworld.txt
Options of echo command
NOTE :- -e here enables the interpretation of backslash escapes.
1. \b : It removes all the spaces in between the text.
For Example:
$ echo -e "echo \bcommand \bLinux"
echocommandLinux
2. \c : Suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.
For Example:
$ echo -e "echo \ccommand Linux"
echo
In above example, text after \c is not printed and omitted trailing new line.
3. \n : This option creates new line from where it is used.
For Example:
$ echo -e "Welcome to \nLinux \noperating system"
Welcome to
Linux
operating system
In the above example,text after \n is printed on next line.
4. \t : This option is used to create horizontal tab spaces.
For Example:
$ echo -e "Welcome \tto \tLinux \toperating \tsystem"
Welcome to Linux operating system
5. \r : Carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.
For Example:
$ echo -e "Welcome to Linux \roperating system"
operating system
In the above example, text before \r is not printed.
6. \v : This option is used to create vertical tab spaces.
For Example:
$ echo -e "Welcome to \vLinux \voperating \vsystem"
Welcome to
Linux
operating
system
7. \a : alert return with backspace interpretor ‘-e‘ to have sound alert.
For Example:
$ echo -e "\aWelcome to Linux operating system"
Welcome to Linux operating system
This command when executed, it will produce an alert sound or Bel .
8. echo * : This command will print all files/folders, similar to “ls” command .
For Example:
$ echo *
Desktop Documents Downloads linux-color-coding Music Pictures Public Templates textfile.txt Videos
$ ls
Desktop Downloads Music Public textfile.txt
Documents linux-color-coding Pictures Templates Videos
9. -n : This option is used to omit echoing trailing newline .
For Example:
$ echo -n "Linux color coding"
Linux color coding