"CLS" Equivalent in BASH?

BashTerminal

Bash Problem Overview


How do you clear the entire terminal in BASH, like the command prompt's cls command?

clear doesn't work because it doesn't actually clear anything, it just scrolls down.

Bash Solutions


Solution 1 - Bash

As far as I know, there isn't a way to do this any better than what clear does with bash.

I think it's a feature that could be built into the terminal you're using though. I know the Mac Terminal app has a 'Clear Scrollback' menu option (command + k) that does what you're asking for.

Solution 2 - Bash

Why don't you try Ctrl+l (control, lowercase "L"). This works in most shells (err terminals)...

In OSX terminal -

Command ⌘+l (command, l) leads to removing last typed command from display.

Command ⌘+k (command, k) leads to removing/clearing all display buffer.

reset (type this in terminal) leads to reset of terminal in case display becomes garbled.

not sure of equivalent in other unix flavors.

Solution 3 - Bash

You're probably looking for the reset command.

However, the scroll-back buffer is not a feature of bash but of the terminal program. You didn't say what terminal program you were using.

Solution 4 - Bash

xterm will allow the escape sequence ESC [3J to clear the scroll back, so you could do:

alias cls="clear; printf '\033[3J'"

Solution 5 - Bash

Short Answer

clear && clear

or

tput reset

Other Ways

Here are all the ways you can clear the terminal screen in Unix:

clear               # only clear visible screen
clear && clear      # clear buffer as well
tput clear          # same as clear but by sending escape seq
reset               # clear + reset internal terminal state + 1sec delay
tput reset          # same as reset but without 1sec delay
stty sane           # don't clear screen but reset some terminal options
echo -e "\033c"     # same as tput reset but hardcoded escape seq
printf "\033c"      # same as tput reset but hardcoded escape seq
setterm -reset      # same as tput reset, setterm has friendlier commands

Long Answer

The clear command only clears the visible screen but not the buffer so you can do Shift+PageUp to scroll up in the terminal and still view previous outputs. If you want to get same result as cls then do clear twice like clear && clear.

Another related command is reset which (I believe) resets the internal state of the terminal program. Unfortunately, this command includes 1 second of delay to support really old terminals. So if you are not ok with that kind of delay then use tput reset which seems to do same thing as reset minus the delay.

But what does tput do? In Unix, you can send terminal all kinds of ASCII character sequences which are interpreted as commands by the terminal. This allows you to do funky things like blink or color the text or turn off echo (during password typing) or set terminal options or do clear or reset. This you can send by tput clear or tput reset. The clear and reset command are equivalent but they run from the binaries that comes with your distro and may do additional stuff. The setterm -reset is similar to tput reset. Setting terminal using setterm is usually better because unlike tput it has more readable options in general case however we here use tput because it's smaller in length :).

You might have also seen people using things like echo -e "\033c" or printf "\033c" which is equivalent to tput reset but the escape sequence is now hard coded. The tput looks up terminal properties and uses the right escape sequence.

Another related command is stty sane which actually doesn't do any screen clearing but it sets many of the terminal options to defaults so if your terminal looks garbled or if terminal stays blank when you type (for example, because you printed binary file to terminal with escape sequence to turn off echo) then this command might help. For extreme garbled terminal cases, you can use all of the available resetting techniques in the sequence. I've alias like this for such occasions:

alias cls='tput reset'
alias clshard='reset; stty sane; tput rs1; setterm -reset; tput reset'

Related

What's the equivalent of the “cls” command from Windows/DOS?

What commands can I use to reset and clear my terminal?

Solution 6 - Bash

Use +K. It removes the entries so I can't scroll up anymore.

So +K to clear everything including scrolling. Ctrl+L to clear terminal window but still be able to see everything when scrolling up.

Solution 7 - Bash

In ~/.bashrc, the perfect cls is:

cls () {
    printf -- '%b' '\033c'
    return $?
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser541686View Question on Stackoverflow
Solution 1 - BashDouglasHeriotView Answer on Stackoverflow
Solution 2 - BashSrikar AppalarajuView Answer on Stackoverflow
Solution 3 - BashzadView Answer on Stackoverflow
Solution 4 - BashRandom832View Answer on Stackoverflow
Solution 5 - BashShital ShahView Answer on Stackoverflow
Solution 6 - BashAmiView Answer on Stackoverflow
Solution 7 - BashMario PalumboView Answer on Stackoverflow