I'm doing that command to measure RAM usage:

while sleep 1; do free && echo -e && ps -eo rss | awk '{sum+=$1} END {print sum}' && echo -e "\n"; done

However, it still keeps printing new outputs instead of erasing/overwriting the last one.

The goal is about making that command looks like top or htop behavior.

So the question is: Without writing a shell script, how do I make a command to refresh itself ?

I've stumbled upon this: https://askubuntu.com/a/684887

So printf "\033c" works, but the problem is it behaving like reset, which clears the whole terminal except the last output.

Besides, watch also works, but quitting it won't keep the last output, like top does.


Is there a way to mix both ? Like mimic-ing top's behavior ?

Top refreshes itself, without resetting the whole terminal and keeps the last output when quitting it.

    Use the watch command. To run the command you've specified, something like:

    watch -n 1 'free -t && echo && ps -eo rss | awk '\''{sum+=$1} END {print sum}'\'' && echo'

    Pay special attention the special quoting around the awk command necessary to put single quotes inside single quotes.

    watch runs the program at a specified interval (1 second here, with the -n 1 flag) and displays the first screen of output on the screen, refreshing each time.

    If you're set on making the output persist after you quit, you can run the command once more at the end. Something like:

    tmp='free -t && echo && ps -eo rss | awk '\''{sum+=$1} END {print sum}'\'' && echo'watch -n 1 "$tmp" ; eval "$tmp"

    Where I'm just using the temporary variable $tmp to avoid typing the whole command twice.

    • So that's why awk didn't worked with watch when I tried... Besides, "echo -e" seems to behave weird since instead of having a \n, I do get "-e" output.– X.LINKJan 14 at 2:17
    • By "-e" output, I meant that it literally output "-e" instead of having a blank line. Also, watch seems to reset itself after being done, while top's output is still there after quitting it; is there a way to mimic that ?– X.LINKJan 14 at 2:35
    • 1
      If you want a blank line, just do echo with no arguments: that prints a newline by default. It's acting like that because watch calls /bin/sh, and -e is a bash extension. I've edited my answer to get rid of the superfluous -e's.– ChrisJan 14 at 2:43
    • 2
      @tjt263 Nope, it's the different interpretation of echo in /bin/sh versus /bin/bash. The POSIX echo doesn't support any options, except possibly -n.– ChrisJan 14 at 2:49
    • 1
      @X.LINK I don't think there's a way to get watch to persist. You could run the command once more after watch quits if you really want to make sure it stays on the screen, I suppose.– ChrisJan 14 at 2:53

    One solution is to avoid printing in subcommands (such as awk) and get everything in variables instead, and then print everything in one line with a CR instead of a LF at the end (I use bash printf for this):

     printf "%10s %4d %4d\r" $dev $recvdiff $senddiff
    • The bad thing is, I can't do that command without awk. Or is there a way to have the total resident RAM usage I'm not aware of ? Also, is there a way to do that with echo since printf is slower in CPU times ?– X.LINKJan 14 at 1:58

    Well, if you don't mind clearing everything else in the terminal window, you could add a clear after the sleep:

    while sleep 1; do clear; free -t && echo -e && ps -eo rss | awk '{sum+=$1} END {print sum}' && echo -e "\n"; done
    • Damn, me always using ctrl-l made me forgot "clear". It does works, but that's a quick and dirty way to do things. I'll take it as a last resort though.– X.LINKJan 14 at 2:07

    Your Answer


    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.