I need some help with shell script. I'm looking for a way to clear the console screen. I have tried to use clear but it does not work.
Asked
Active
Viewed 5,375 times
1
-
@anubhava `reset` does more than clear the screen and I don't think it should be used. – trojanfoe Jun 07 '14 at 15:00
-
In normal conditions `clear` should have worked too so if that is not working for reason one needs more potent weapon. – anubhava Jun 07 '14 at 15:01
-
Does this answer your question? [How to clear previous output in Terminal in Mac OS X?](https://stackoverflow.com/questions/2198377/how-to-clear-previous-output-in-terminal-in-mac-os-x) – fearless_fool Jul 15 '20 at 17:33
2 Answers
2
Odd. Using clear
works for me:
#/usr/bin/sh
echo 123
clear
Then:
$ chmod +x foo.sh
$ ./foo.sh
...clears the screen in Terminal Version 2.4 under OSX 10.9.3.

fearless_fool
- 33,645
- 23
- 135
- 217
-
Three questions: what version of Terminal? What version of OS X? And what does "not work" mean? (In Terminal, clear doesn't totally erase the screen; it scrolls the previous output up so it's not visible, but you can scroll back to it.) – fearless_fool Jun 07 '14 at 23:41
1
You could use AppleScript from the command shell to simulate pressing command-K in the Terminal (which corresponds to the "Clear All" menu item):
open -a 'Terminal.app'; osascript \
-e 'tell application "System Events"' \
-e 'tell process "Terminal" to keystroke "k" using command down' \
-e 'end tell'

Ivan X
- 2,165
- 16
- 25