code_cowboy
code_cowboy

Reputation: 723

Clear PowerShell console in bash on Windows

I download bash.exe from SourceForge and added it to my path in Powershell, but I can't get it to clear the console. clear.exe is missing from the zipfile that was downloaded, so it makes sense that that command doesn't work. However, using Ctrl+L also does not clear the powershell console.

How can I get the powershell console to clear when I'm using bash in it?

Note: I've tried adding an alias called clear to my .bashrc as alias clear=echo <many enters>, but it doesn't work quite the way I've expected (i.e. only echoes 4 or 5 newlines). Also, echo "\n\n" just prints out literal \n\n.

Upvotes: 1

Views: 977

Answers (1)

mklement0
mklement0

Reputation: 440536

In the absence of a clear or tput utility, and given that the usual ANSI escape sequences don't work with the (built-in) printf, you must call out to either cmd.exe or PowerShell to effect clearing the screen:

bash$ powershell -noprofile -c cls

Using cmd is faster, but the problem is that the win-bash invokes external programs by double-quoting each argument behind the scenes, which causes a command such as cmd /c cls to malfunction; the following workaround mostly works, but prints the cmd.exe prompt string once after clearing the screen.

# !! Clears the screen, but prints the cmd.exe prompt string once.
bash$ echo cls | cmd

Upvotes: 2

Related Questions