Tigger has a good answer about what the \r
does. One other trick for the Bourne shell (which works in different ways in other shells) is to clear the end of the line like so:
#/bin/sh -e
echo "this is a first line...\r\c"
...other code...
echo "another line\033[K\r\c"
Without the \033[K
(kill the end of the line), you would get:
another lineirst line...
With the \033[K
the output from the cursor position to the end of the line is cleared so the result of the above is:
another line
This is practical for scripts that take a long time and you want to let the user know where you're at (i.e. maybe a path your script is working on and the next path is shorter).
Note: As a side note, I'd like to specify that the \n
is what sends your cursor to the next line. In the old MS-DOS console, it just goes one line down, same column. This is why under MS-Windows you are still expected to write both characters (\r\n
). Although in most cases people just write \n
and things work as expected because the FILE
has a "text mode" which inserts a \r
before each \n
(and also mangles binary files every so often...).
␣my college
, just like the first one. – Kusalananda Jul 01 '17 at 08:31echo -e
is notoriously unportable. You shoud probably useprintf
instead. – tripleee Jul 01 '17 at 10:39