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.
Key Takeaways
- Exploring how to use Bash script to clear the screen.
- Knowing about the use of ASCII code to clear the screen.
Free Downloads
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,
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.
➊ 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 clear_Screen1.sh
- nano: Opens and creates a file in the nano text editor.
- clear_Screen1.sh: File name.
➌ After that, copy the following script into your text editor.
#!/bin/bash
# Reading user input to clear the screen
echo "Press Enter to clear the screen."
read
# Clear the screen
clear
# Show users that the screen was cleared
echo "Screen 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 clear_Screen1.sh
- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- clear_Screen1.sh: File that you want to make executable.
➏ Finally, you can simply run the file from your command line by executing:
./clear_Screen1.sh
After running the script, the prompt is waiting for user input.
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.
Script (clear_Screen2.sh) >
#!/bin/bash
# Clearing screen using ANSI escape sequences
printf "\033c"
# Show users that the screen was cleared
echo "Screen cleared!"
Running the script to clear the screen.
./clear_Screen2.sh
After running the script, the screen is cleared.
Comparative Analysis of the Methods
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 |
|
|
Method 2 |
|
|
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.
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
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial