Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backspacing in Bash

Tags:

bash

backspace

How do you backspace the line you just wrote with bash and put a new one over its spot? I know it's possible, Aptitude (apt-get) use it for some of the updating stuff and it looks great.

like image 400
Kyle Hotchkiss Avatar asked Jan 10 '11 06:01

Kyle Hotchkiss


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

How do I Backspace on Linux?

The Linux kernel default lets Ctrl-Backspace generate BackSpace - this is sometimes useful as emergency escape, when you find you can only generate DELs. The left Alt key is sometimes called the Meta key, and by default the combinations AltL-X are bound to the symbol MetaX.

What is backslash in bash?

A non-quoted backslash ' \ ' is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline .


2 Answers

Try this:

$ printf "12345678\rABC\n"
ABC45678

As you can see, outputting a carriage return moves the cursor to the beginning of the same line.

You can clear the line like this:

$ printf "12345678\r$(tput el)ABC\n"
ABC

Using tput gives you a portable way to send control characters to the terminal. See man 5 terminfo for a list of control codes. Typically, you'll want to save the sequence in a variable so you won't need to call an external utility repeatedly:

$ clear_eol=$(tput el)
$ printf "12345678\r${clear_eol}ABC\n"
ABC
like image 88
Dennis Williamson Avatar answered Sep 28 '22 04:09

Dennis Williamson


It's not really clear to me what you want, but, depending on your terminal settings you can print ^H (control H) to the screen and that will back the cursor up one position.

Also note that some terminals have the ability to move the cursor to the beginning of the line, in which case you'd move to the beginning of the line, print enough spaces to overwrite the entire line (Usually available from $COLUMNS) and then print any message or whatever.

If you clarify exactly what you want and I can answer you I'll update my answer.

like image 29
JimR Avatar answered Sep 28 '22 04:09

JimR