Print something in console in the same place of the previous echo, with a sort of negative echo
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
In bash you can cast a command named clear
to clear all the screen commands.
And with echo
you can print whatever you want onscreen..
In my simple scripts I often have the need of print a percentage of what's being done with my commands..
So I could do something like..
echo "89%"
echo "90%"
echo "91%"
and so on..
what I hate is getting the screen full of percent updates...
89%
90%
91%
...
what I would like is to learn if there's a special character combination (eg. "33[01;31m") that could be echoed with bash or php echo and tells the console "remove the last previous printed character.."
doing so by using something like: (php example)
echo str_repeat($neg_character, strlen($last_percentage_update_string));
echo $new_percentage_update_string;
I would get the new string printed at the exact position of the previous one without have the screen full of lines
Otherwise I look for an approach to do the same in other ways always using bash and php scripts (please include actual working examples at least with a debian9 console and php7)
linux bash php console
add a comment |Â
up vote
6
down vote
favorite
In bash you can cast a command named clear
to clear all the screen commands.
And with echo
you can print whatever you want onscreen..
In my simple scripts I often have the need of print a percentage of what's being done with my commands..
So I could do something like..
echo "89%"
echo "90%"
echo "91%"
and so on..
what I hate is getting the screen full of percent updates...
89%
90%
91%
...
what I would like is to learn if there's a special character combination (eg. "33[01;31m") that could be echoed with bash or php echo and tells the console "remove the last previous printed character.."
doing so by using something like: (php example)
echo str_repeat($neg_character, strlen($last_percentage_update_string));
echo $new_percentage_update_string;
I would get the new string printed at the exact position of the previous one without have the screen full of lines
Otherwise I look for an approach to do the same in other ways always using bash and php scripts (please include actual working examples at least with a debian9 console and php7)
linux bash php console
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
In bash you can cast a command named clear
to clear all the screen commands.
And with echo
you can print whatever you want onscreen..
In my simple scripts I often have the need of print a percentage of what's being done with my commands..
So I could do something like..
echo "89%"
echo "90%"
echo "91%"
and so on..
what I hate is getting the screen full of percent updates...
89%
90%
91%
...
what I would like is to learn if there's a special character combination (eg. "33[01;31m") that could be echoed with bash or php echo and tells the console "remove the last previous printed character.."
doing so by using something like: (php example)
echo str_repeat($neg_character, strlen($last_percentage_update_string));
echo $new_percentage_update_string;
I would get the new string printed at the exact position of the previous one without have the screen full of lines
Otherwise I look for an approach to do the same in other ways always using bash and php scripts (please include actual working examples at least with a debian9 console and php7)
linux bash php console
In bash you can cast a command named clear
to clear all the screen commands.
And with echo
you can print whatever you want onscreen..
In my simple scripts I often have the need of print a percentage of what's being done with my commands..
So I could do something like..
echo "89%"
echo "90%"
echo "91%"
and so on..
what I hate is getting the screen full of percent updates...
89%
90%
91%
...
what I would like is to learn if there's a special character combination (eg. "33[01;31m") that could be echoed with bash or php echo and tells the console "remove the last previous printed character.."
doing so by using something like: (php example)
echo str_repeat($neg_character, strlen($last_percentage_update_string));
echo $new_percentage_update_string;
I would get the new string printed at the exact position of the previous one without have the screen full of lines
Otherwise I look for an approach to do the same in other ways always using bash and php scripts (please include actual working examples at least with a debian9 console and php7)
linux bash php console
edited Aug 24 at 9:17
asked Aug 23 at 10:14
user3450548
83911128
83911128
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
17
down vote
The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return (r
):
printf "89%%"; sleep 1; printf "r90%%n"
Note that this doesnâÂÂt clear the line, so you need to take care of that if necessary. Simple options are adding spaces to the end, or making the output fixed-width (e.g. printf "%2d%%n" 1
gives a leading space).
There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). For example
printf "89%%"; sleep 1; printf "e[3D90%%n"
uses âÂÂ[3D
to move three characters to the left, and writes over them (assuming your printf
supports e
);
printf "89%% hello"; sleep 1; printf "e[0Ee[K90%%n"
uses âÂÂ[0E
to move to the beginning of the current line, and âÂÂ[K
to clear to the end of the line (assuming your terminal supports those sequences).
tput
provides a terminal- and printf
-agnostic way of accessing these sequences:
printf "89%%"; sleep 1; tput cub 3; tput el; printf "90%%n"
will move the cursor to the left three times (cub 3
) and clear to the end of the line (el
), using whatever character sequence is appropriate for the current terminal;
printf "89%% hello"; sleep 1; tput hpa 0; tput el; printf "90%%n"
will move the cursor to the left-most column (hpa 0
) and clear to the end of the line.
man terminfo
will tell you what âÂÂcapability nameâ to use with tput
.
(Note that a lot of the specifics of the examples above assume that all your output is on the same line. TheyâÂÂre not supposed to be fool-proof, only to illustrate various approaches.)
For similar screen control in PHP scripts, you could look at the PECL ncurses
extension.
5
One can usually usetput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (andtput el
to erase the line).
â Stéphane Chazelas
Aug 23 at 10:29
1.e
isnt portable 2. neither ise[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifyinghpa
!
â Stephen Kitt
Aug 23 at 15:32
One could useech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that100
is 3 digits long, by the way. (-:
â JdeBP
Aug 23 at 16:53
 |Â
show 9 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return (r
):
printf "89%%"; sleep 1; printf "r90%%n"
Note that this doesnâÂÂt clear the line, so you need to take care of that if necessary. Simple options are adding spaces to the end, or making the output fixed-width (e.g. printf "%2d%%n" 1
gives a leading space).
There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). For example
printf "89%%"; sleep 1; printf "e[3D90%%n"
uses âÂÂ[3D
to move three characters to the left, and writes over them (assuming your printf
supports e
);
printf "89%% hello"; sleep 1; printf "e[0Ee[K90%%n"
uses âÂÂ[0E
to move to the beginning of the current line, and âÂÂ[K
to clear to the end of the line (assuming your terminal supports those sequences).
tput
provides a terminal- and printf
-agnostic way of accessing these sequences:
printf "89%%"; sleep 1; tput cub 3; tput el; printf "90%%n"
will move the cursor to the left three times (cub 3
) and clear to the end of the line (el
), using whatever character sequence is appropriate for the current terminal;
printf "89%% hello"; sleep 1; tput hpa 0; tput el; printf "90%%n"
will move the cursor to the left-most column (hpa 0
) and clear to the end of the line.
man terminfo
will tell you what âÂÂcapability nameâ to use with tput
.
(Note that a lot of the specifics of the examples above assume that all your output is on the same line. TheyâÂÂre not supposed to be fool-proof, only to illustrate various approaches.)
For similar screen control in PHP scripts, you could look at the PECL ncurses
extension.
5
One can usually usetput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (andtput el
to erase the line).
â Stéphane Chazelas
Aug 23 at 10:29
1.e
isnt portable 2. neither ise[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifyinghpa
!
â Stephen Kitt
Aug 23 at 15:32
One could useech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that100
is 3 digits long, by the way. (-:
â JdeBP
Aug 23 at 16:53
 |Â
show 9 more comments
up vote
17
down vote
The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return (r
):
printf "89%%"; sleep 1; printf "r90%%n"
Note that this doesnâÂÂt clear the line, so you need to take care of that if necessary. Simple options are adding spaces to the end, or making the output fixed-width (e.g. printf "%2d%%n" 1
gives a leading space).
There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). For example
printf "89%%"; sleep 1; printf "e[3D90%%n"
uses âÂÂ[3D
to move three characters to the left, and writes over them (assuming your printf
supports e
);
printf "89%% hello"; sleep 1; printf "e[0Ee[K90%%n"
uses âÂÂ[0E
to move to the beginning of the current line, and âÂÂ[K
to clear to the end of the line (assuming your terminal supports those sequences).
tput
provides a terminal- and printf
-agnostic way of accessing these sequences:
printf "89%%"; sleep 1; tput cub 3; tput el; printf "90%%n"
will move the cursor to the left three times (cub 3
) and clear to the end of the line (el
), using whatever character sequence is appropriate for the current terminal;
printf "89%% hello"; sleep 1; tput hpa 0; tput el; printf "90%%n"
will move the cursor to the left-most column (hpa 0
) and clear to the end of the line.
man terminfo
will tell you what âÂÂcapability nameâ to use with tput
.
(Note that a lot of the specifics of the examples above assume that all your output is on the same line. TheyâÂÂre not supposed to be fool-proof, only to illustrate various approaches.)
For similar screen control in PHP scripts, you could look at the PECL ncurses
extension.
5
One can usually usetput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (andtput el
to erase the line).
â Stéphane Chazelas
Aug 23 at 10:29
1.e
isnt portable 2. neither ise[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifyinghpa
!
â Stephen Kitt
Aug 23 at 15:32
One could useech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that100
is 3 digits long, by the way. (-:
â JdeBP
Aug 23 at 16:53
 |Â
show 9 more comments
up vote
17
down vote
up vote
17
down vote
The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return (r
):
printf "89%%"; sleep 1; printf "r90%%n"
Note that this doesnâÂÂt clear the line, so you need to take care of that if necessary. Simple options are adding spaces to the end, or making the output fixed-width (e.g. printf "%2d%%n" 1
gives a leading space).
There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). For example
printf "89%%"; sleep 1; printf "e[3D90%%n"
uses âÂÂ[3D
to move three characters to the left, and writes over them (assuming your printf
supports e
);
printf "89%% hello"; sleep 1; printf "e[0Ee[K90%%n"
uses âÂÂ[0E
to move to the beginning of the current line, and âÂÂ[K
to clear to the end of the line (assuming your terminal supports those sequences).
tput
provides a terminal- and printf
-agnostic way of accessing these sequences:
printf "89%%"; sleep 1; tput cub 3; tput el; printf "90%%n"
will move the cursor to the left three times (cub 3
) and clear to the end of the line (el
), using whatever character sequence is appropriate for the current terminal;
printf "89%% hello"; sleep 1; tput hpa 0; tput el; printf "90%%n"
will move the cursor to the left-most column (hpa 0
) and clear to the end of the line.
man terminfo
will tell you what âÂÂcapability nameâ to use with tput
.
(Note that a lot of the specifics of the examples above assume that all your output is on the same line. TheyâÂÂre not supposed to be fool-proof, only to illustrate various approaches.)
For similar screen control in PHP scripts, you could look at the PECL ncurses
extension.
The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return (r
):
printf "89%%"; sleep 1; printf "r90%%n"
Note that this doesnâÂÂt clear the line, so you need to take care of that if necessary. Simple options are adding spaces to the end, or making the output fixed-width (e.g. printf "%2d%%n" 1
gives a leading space).
There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). For example
printf "89%%"; sleep 1; printf "e[3D90%%n"
uses âÂÂ[3D
to move three characters to the left, and writes over them (assuming your printf
supports e
);
printf "89%% hello"; sleep 1; printf "e[0Ee[K90%%n"
uses âÂÂ[0E
to move to the beginning of the current line, and âÂÂ[K
to clear to the end of the line (assuming your terminal supports those sequences).
tput
provides a terminal- and printf
-agnostic way of accessing these sequences:
printf "89%%"; sleep 1; tput cub 3; tput el; printf "90%%n"
will move the cursor to the left three times (cub 3
) and clear to the end of the line (el
), using whatever character sequence is appropriate for the current terminal;
printf "89%% hello"; sleep 1; tput hpa 0; tput el; printf "90%%n"
will move the cursor to the left-most column (hpa 0
) and clear to the end of the line.
man terminfo
will tell you what âÂÂcapability nameâ to use with tput
.
(Note that a lot of the specifics of the examples above assume that all your output is on the same line. TheyâÂÂre not supposed to be fool-proof, only to illustrate various approaches.)
For similar screen control in PHP scripts, you could look at the PECL ncurses
extension.
edited Aug 23 at 19:07
answered Aug 23 at 10:16
Stephen Kitt
143k22312377
143k22312377
5
One can usually usetput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (andtput el
to erase the line).
â Stéphane Chazelas
Aug 23 at 10:29
1.e
isnt portable 2. neither ise[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifyinghpa
!
â Stephen Kitt
Aug 23 at 15:32
One could useech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that100
is 3 digits long, by the way. (-:
â JdeBP
Aug 23 at 16:53
 |Â
show 9 more comments
5
One can usually usetput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (andtput el
to erase the line).
â Stéphane Chazelas
Aug 23 at 10:29
1.e
isnt portable 2. neither ise[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifyinghpa
!
â Stephen Kitt
Aug 23 at 15:32
One could useech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that100
is 3 digits long, by the way. (-:
â JdeBP
Aug 23 at 16:53
5
5
One can usually use
tput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (and tput el
to erase the line).â Stéphane Chazelas
Aug 23 at 10:29
One can usually use
tput cub 3
to move the cursor back by 3 columns without having to hardcode the sequence (and tput el
to erase the line).â Stéphane Chazelas
Aug 23 at 10:29
1.
e
isnt portable 2. neither is e[0E
â Steven Penny
Aug 23 at 11:37
1.
e
isnt portable 2. neither is e[0E
â Steven Penny
Aug 23 at 11:37
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
Thanks @Steven, IâÂÂd mentioned that the sequences were terminal-dependent but IâÂÂve added some more qualifiers.
â Stephen Kitt
Aug 23 at 11:40
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifying
hpa
!â Stephen Kitt
Aug 23 at 15:32
@Toby none, my test was bad :-/. Thanks for pointing that out, and for identifying
hpa
!â Stephen Kitt
Aug 23 at 15:32
One could use
ech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that 100
is 3 digits long, by the way. (-:â JdeBP
Aug 23 at 16:53
One could use
ech
for erasure. And moving backwards by 3 positions has a gotcha, and two possible optimizations that full-screen programs tend to use. But both optimizations and erasure are overkill for simple current-line-only terminal stuff, when one follows that advice to use a fixed-width format specifier. Don't forget that 100
is 3 digits long, by the way. (-:â JdeBP
Aug 23 at 16:53
 |Â
show 9 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464357%2fprint-something-in-console-in-the-same-place-of-the-previous-echo-with-a-sort-o%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password