bash – echo command with option to display the output on the same line.

Here is a nice little trick to display the output in the same line :

1
 echo -ne \"output\\r\"

The trick here is simple, first we disable the newline printed by echo command with the \”-n\” option and then we enable the interpretation of the backslash characters with the \”-e\” option. As the \”\\r\” is used as carriage return which brings you to the start of the line without the newline (like in typewriter), we will use the same to get the desired effect. So, if you wanted a stopwatch for a 100 seconds on the console then this simple bash for loop would do the same:

1
2
3
4
5
6
7
for i in `seq 100 1`

do

echo -ne \"$i\\r\"

done
\"bash

Have fun playing with this and comment if you find some other interesting use of the same.

\"Enhanced