back to listing index

How to delete line with echo?

[web search]
Original source (unix.stackexchange.com)
Tags: bash shell-scripting
Clipped on: 2016-06-14

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..

asked Dec 11 '11 at 11:24
Image (Asset 2/8) alt=
LanceBaynes
5,35349148276
1 upvote
  flag
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 '14 at 14:33
1 upvote
  flag
I'm pretty sure you can just use echo -ne "\e[2K\r". But ANSI escape sequences FTW, nonetheless. – Parthian Shot Feb 4 '15 at 18:15
up vote 35 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.
answered Dec 11 '11 at 11:37
Image (Asset 4/8) alt=
lesmana
8,19953354
   upvote
  flag
Yes, but echo -e 'fooooooooo\rbar echoes barooooooo. – Mat Dec 11 '11 at 11:40
1 upvote
  flag
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
   upvote
  flag
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.

answered Dec 11 '11 at 20:50
Image (Asset 5/8) alt=
Kevin
17.8k54082
6 upvote
  flag
This assumes a VT100-compatible terminal or emulator (which is a pretty safe assumption these days). – Keith Thompson Dec 12 '11 at 1:46
   upvote
  flag
If you want to erase something you just echoed, your previous echo must have the -n flag, or you'll be sent to a new line – djjeck Nov 20 '14 at 1:45
   upvote
  flag
If you want to write something else, on the line you're erasing, add a -n flag on the echo above, or else you'll go to a new line right after deleting it. Also, append a \r to the string above, or your new content won't go to the beginning of that line. – djjeck Nov 20 '14 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 -ne "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. The -n makes echo not add a newline after the foobar.

answered Dec 11 '11 at 18:16
Image (Asset 7/8) alt=
Arcege
12.3k12445
   upvote
  flag
I like the sort of future-proofing part of this specific solution - using tput possibly means that it'll work on any kind of terminal. – Amos Shapira Nov 6 '14 at 1:36

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
answered Dec 11 '11 at 16:35
Image (Asset 8/8) alt=
kojiro
1,62711022
   upvote
  flag
perfect! exactly what I needed – Omar A. Shaban Mar 26 '13 at 17:09
   upvote
  flag
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

asked

4 years ago

viewed

30335 times

active

3 months ago

Hot Meta Posts

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.6.13.3668
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.