Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that I could delete the last three chars with:

echo -ne '\b\b\b'

But how can I delete a full line? I mean I dont want to use:

echo -ne '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b'

...etc... to delete a long line..

share|improve this question
    
For those who'd like to continuously write on the same line: echo -ne "\033[2K" ; printf "\r", now the line is good as new, as if it was never written to before. –  Hello World Oct 18 at 14:33

4 Answers 4

up vote 17 down vote accepted

You can use \r to move the "cursor" to the beginning of the line. Note that neither \b nor \r deletes the printed characters. It just moves the "cursor" back one character and to the beginning of the line, respectively.

Example: both

echo -e 'foooo\b\b\b\b\bbar'

and

echo -e 'foooo\rbar'

will print:

baroo

If you want the characters deleted then you have to use the following workaround:

echo -e 'fooooo\r     \rbar'

output:

bar  

Excerpt from man echo:

   If -e is in effect, the following sequences are recognized:

   \0NNN  the character whose ASCII code is NNN (octal)

   \\     backslash

   \a     alert (BEL)

   \b     backspace

   \c     produce no further output

   \f     form feed

   \n     new line

   \r     carriage return

   \t     horizontal tab

   \v     vertical tab

   NOTE: your shell may have its own version of echo, which usually super‐
   sedes the version described here.  Please refer to your  shell's  docu‐
   mentation for details about the options it supports.
share|improve this answer
    
Yes, but echo -e 'fooooooooo\rbar echoes barooooooo. –  Mat Dec 11 '11 at 11:40
1  
Yes you are right. Note that echo -e 'foooo\b\b\b\b\b\b\b\b\b\bbar' also echoes baroo. –  lesmana Dec 11 '11 at 11:46
    
So sweet, works even flawlessly in printf. –  Michael-O Jun 6 '13 at 15:07

You're looking for terminal escapes. In particular, to clear from the cursor position to the beginning of the line:

echo -e "\033[1K"

Or everything on the line, regardless of cursor position:

echo -e "\033[2K"

And you can do all sorts of other neat tricks with terminal escapes too.

share|improve this answer
3  
This assumes a VT100-compatible terminal or emulator (which is a pretty safe assumption these days). –  Keith Thompson Dec 12 '11 at 1:46

If you want to clear the line, then I suggest you use a combination of the carriage return people are mentioning and terminfo.

ceol=`tput el` # terminfo clr_eol
echo -e "xyzzyxyzzy\r${ceol}foobar"

This will write "xyzzyxyzzy", then return to the beginning of the line and send the "clear to end of line" sequence to the terminal, then write "foobar".

share|improve this answer

You explicitly ask for echo, but this request pins you down. Here's an approach that uses bash's builtin printf command with brace expansion:

printf 'fooooooooo' # 10 characters
printf '\r'; printf ' %0.s' {0..9} # 10 expansions of the space character
share|improve this answer
    
perfect! exactly what I needed –  Omar A. Shaban Mar 26 '13 at 17:09
    
printf 'Status: started';sleep 2s;printf '\r';printf 'Status: updated';sleep 2s;printf '\r'; –  Omar A. Shaban Mar 26 '13 at 17:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.