How to Clear Screen Using Bash Script? [2 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. In this article, I am going to show you how to clear the screen using the Bash script.

2 Methods to Clear Screen Using Bash Scripts

In this article, I am going to show two methods of cleaning the screen using Bash scripting. Both methods use commands to clear the screen. In the first method, you are going to see the use of the clear command and in the second method, you are going to use ASCII control characters.

You can read our Comparative Analysis of Methods to distinguish between these three methods and pick the best one for your needs.

Method 1: Using “clear” Command to Clean Screen With Bash Script

In this method, I am going to use the clear command in a script to clear the terminal. Also, it takes user input before clearing the screen, so that the script becomes more interactive.

Steps to Follow >

➊ First, launch the Ubuntu terminal. You can use the shortcut keys CTRL+ALT+T to do so.

➋ Then create a file with the extension .sh using the nano command and open the script in the text editor like the following.

  • nano: Opens and creates a file in the nano text editor.
  • File name.

➌ After that, copy the following script into your text editor.


# Reading user input to clear the screen
echo "Press Enter to clear the screen."

# Clear the screen

# Show users that the screen was cleared
echo "Screen cleared!"

The code starts with #!/bin/bash, which specifies the Bash interpreter. Then the next two lines print a message for the user with the echo command and take input using the read command before clearing the screen. Then the command clear is used to clear the screen. After that, a message is printed informing that the screen was cleared.

➍ To save and exit the text editor press CTRL+ O and CTRL+X.

➎ Now, you need to make the bash script file executable. Execute the following command in the terminal to do so.

chmod +x
  • chmod: Changes the permissions of files and directories.
  • +x: Argument with the chmod command to add the executable permission.
  • File that you want to make executable.

➏ Finally, you can simply run the file from your command line by executing:


Clearing screen using clear commandAfter running the script, the prompt is waiting for user input.Screen is cleared after pressing enter After pressing Enter, the screen is cleared.

Method 2: Using ASCII Control Characters to Clear Screen With Bash Script

In this method, I will show you how to clear the screen using ASCII control characters. This method will use ASCII escape characters, which are represented by escape codes. So, if you are familiar with the ASCII escape codes, this method is also suitable for you. You just have to run the script to clear your screen.

You can follow the steps mentioned in method 1 to learn how to write, execute, and run the bash script.

Script ( >


# Clearing screen using ANSI escape sequences
printf "\033c"

#  Show users that the screen was cleared
echo "Screen cleared!"

Just like the previous script, this code starts with #!/bin/bash, which specifies the Bash interpreter. After that, the following line sends the escape sequence for clearing the screen. This sequence is recognized by the terminal emulator, which then clears the console screen, effectively removing all previous output and starting with a blank screen. Then as in the previous code, a clear message is sent.

Running the Bash script to clear screenRunning the script to clear the screen:


Screen is cleared after running the Bash scriptAfter running the script, the screen is cleared.

Comparative Analysis of the Methods for Clearing Screen

In this section, I will give you a comparative analysis of these two methods, mentioned above, so that you can understand which will be best for you to use.

Methods Pros Cons
Method 1
  • This method is relatively simple because you only have to know one simple command.
  • You have to memorize the command.
Method 2
  • Numerous terminal emulators and operating systems offer the ASCII escape character for clearing the screen. This guarantees the portability and compatibility of your Bash script.
  • You have to remember the code to write down the script.

Any user can use the first method easily, as the clear command is very widely used by Bash users. On the other hand, the ASCII escape sequence can be used by those who are familiar with the escape sequences.


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)
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