echo command in Unix | Linux Tutorial

echo command in Unix

echo command is used to display a message on the screen.

echo "Hello"
Hello

We can also use the echo command without quotes (" ") if our string does not contain any space.

echo Hello
Hello

Quotes are necessary if our string contains an space.

echo "Hello World"
Hello World

We can also display the value of a variable with the echo.

echo $var

We can also use escape characters with the echo command.

\- inserts a new line
\- inserts a tab
\- holds the cursor
\r -  carriage return

echo "Hello \n World"
Hello
World

echo "Hello \t World"
Hello     World

echo "Hello World\c"
Hello World _
(cursor blinks after the string)

echo "Hello \r Hi"
Hillo
(It returns the cursor to the start of the line)

Note :- In some flavors of Linux we have to use -e for the escape characters to work.

echo -e "Hello World \n"
Hello World

Comments