How to Clear Screen Using Bash Script? [3 Effective Methods]

To maintain a clutter-free and organized workspace, clearing the screen is very important. You can use the command-line interface for that, but writing a Bash script and executing it to clear the screen can be useful when you have to do this task repetitively.

Follow the below methods to clear screen in Bash terminal:

1. Using “clear” Command

The clear command is used in Bash (and in many other Unix-like shells) to clear the terminal screen. When you enter clear and press ENTER in the terminal, it scrolls the screen and clears it, giving you a fresh prompt at the top of the terminal window.

clear

After running the command, the terminal screen will be cleared in your Linux/Unix.

2. Using ANSI Scape Sequence

ANSI escape sequences are special sequences of characters used to control the formatting, color, and other aspects of text output in terminal environments. The command of ANSI escape sequence for clearing the screen in Bash is :

printf "\033c"

After running the command, the terminal screen will be cleared in your Linux/Unix.

3. Applying Keyboard Shortcut of Clearing Screen

the keyboard shortcut to clear the screen is CTRL+ L. Pressing CTRL with L will scroll the terminal and clear the screen, providing a clean slate for new commands or output. This shortcut is widely supported across various Unix/Linux distributions and is a quick and convenient way to clear the screen without needing to type any commands.

Conclusion

In conclusion, you can see that using a Bash script to clear the screen gives you benefits over repeatedly running the same command. The fact that you will use it or not depends on how complicated and long your task is. If you need to frequently clear the screen, it would be a good idea to use the Bash script. Feel free to comment if you have any further inquiries about this topic.

People Also Ask

How do I clear my screen in Linux command line?
First, you have to open the terminal, and from the command line, just simply type clear. After entering the command, you’ll see that your terminal screen is cleared.

How do I clear a terminal using shortcut key in bash?
You can type from your keyboard, CTRL + L, which is equivalent to the clear command, in order to clear your terminal.

How do I clear all bash terminals?
You can use the command clear to clear all Bash terminals, also you can use CTRL + L or CTRL + SHIFT + K as shortcut keys to clear terminals in Bash.

How do I clear the last command in Bash?
To delete a particular command, you can use this command history -d <line number>. Just insert the line number; in this case, it will be the last line number, and the command will be deleted.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

5/5 - (4 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Lamisa Musharrat

Hello there. My name is Lamisa Musharat, and I'm an Linux Content Developer Executive at SOFTEKO. I earned a bachelor's degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology (BUET).I learned Linux out of my curiosity and now I find it useful as automation is easier using Linux. I take great pleasure in assisting others with Linux-related issues. I really want you to enjoy and benefit from my efforts.Read Full Bio

Leave a Comment