Backspacing in Bash

How do you back out the line you are just writing with bash and put a new one over its spot? I know that maybe Aptitude (apt-get) uses it for some updates, and it looks great.

+4
source share
3 answers

Try the following:

$ printf "12345678\rABC\n" ABC45678 

As you can see, carriage return returns the cursor to the beginning of the same line.

You can clear the line as follows:

 $ 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. As a rule, you need to save the sequence in a variable, so you will not need to repeatedly call an external utility:

 $ clear_eol=$(tput el) $ printf "12345678\r${clear_eol}ABC\n" ABC 
+11
source

I don’t quite understand what you want, but depending on the terminal settings, you can type ^ H (control H) on the screen, and this will return the cursor up one position.

Also note that some terminals have the ability to move the cursor to the beginning of the line, and in this case you will go to the beginning of the line, type enough spaces to rewrite the entire line (usually available from $ COLUMNS), and then print any message or something yet.

If you specify exactly what you want, and I can answer you, I will update my answer.

+1
source

Here's an example of using the find command and the while-read loop to continuously print the full paths to the stdout file in only one line:

 command find -x / -type f -print0 2>/dev/null | while read -d $'\0' filename; do let i+=1 filename="${filename//[[:cntrl:]]/}" # remove control characters such as \n, \r, ... if [[ ${#filename} -lt 85 ]]; then printf "\r\e[0K\e[1;32m%s\e[0m %s" "${i}" "${filename}" else printf "\r\e[0K\e[1;32m%s\e[0m %s" "${i}" "${filename:0:40}.....${filename: -40}" fi done; echo 
+1
source

Source: https://habr.com/ru/post/1334886/


All Articles