I was practicing echo command with option \r
(carriage return) as below.
echo -e "This is \r my college"
output:
my college
but when I add one more word before \r
as below
echo -e "This is the \r my college"
Then it gives me output like:
my college the
Another example
echo -e "This is \r valid data"
valid data
echo -e "This is not a \r valid data"
valid data a
So, I wanted to know that what is the actual purpose of carriage return here?
Best Answer
The
\r
is just that, a "carriage return" - nothing else. This means what is after the\r
will overwrite the beginning of what has already been printed.For example:
Will display:
echo
has printed1234
then gone back to the begining of the line and printed56
over the top of12
.For a new line, try
\n
instead. No need for spaces too. For example:Which will output:
The carriage return is useful for things like this:
Which will display
date
over the top of itself instead of creating a new line after each loop.