Cyberithub

How to Suppress all the Output of a Linux Bash Shell Script{4 Best Methods}

Do you want to suppress or hide all the output of a Linux Bash Shell Script ? Do you want to redirect all the output to /dev/null ? Are you looking to silence all the output of a Linux Bash Shell Script ? Are you looking to redirect all the error to output ? Do you want to know how to redirect all the output of Bash Shell script to a file ? If yes then you have reached the correct place.

Here I am going to explain all the different methods that can be used to achieve these tasks. You can use all the below explained methods to not just suppress or hide the output of Linux bash shell script from command line but from the crontab as well. So stay focused!!!

How to Suppress or Hide all the Output of a Linux Bash Shell Script

How to Suppress or Hide all the Output of a Linux Bash Shell Script

Also Read: 25 Practical and Useful rpm command examples in Linux{cheatsheet}

Method 1: How to Suppress or Hide all the output of a Linux bash shell script

If you are looking to suppress or hide all the output of a bash shell script from Linux command line as well as from the crontab then you can simply redirect all the output to a file known as /dev/null. This file is known as Black Hole which will engulf everything you give without complaining. Hence this is the best place to redirect all the output. In this method we are sending example.sh script output to /dev/null.

[root@localhost ~]# ./example.sh > /dev/null

Method 2: How to Suppress or Hide all the Error messages from a Linux Shell Script 

Using above method you can easily suppress all the output of a Bash Shell Script but what about the script error messages if there is any ? Error messages will not suppressed by above method. For that you need to use another method where you first need to redirect all the error messages to the output and then redirect all the output to /dev/null using earlier explained method. In this method we are sending example.sh script output and its error messages to /dev/null. 2>&1 simply means send the error to the same place output is being sent.

[root@localhost ~]# ./example.sh > /dev/null 2>&1

Method 3: How to Redirect All the output of a Bash Shell Script to a File

If you think output of the Bash Script will be needed later then instead of redirecting all the output of the script to /dev/null you can redirect and save it in a file for later use. You can check more on Bash Guide for Beginners. In this method we are redirecting example.sh script output to file.log.

[root@localhost ~]# ./example.sh > file.log

Method 4: How to Redirect All the error messages of Bash Shell Script to a File

Similarly for the error messages also you can redirect all the error messages to a file instead of redirecting it to /dev/null from where messages cannot be retrieved back. This is really important in the production or critical cases where you are running a bash shell script in crontab and suddenly some error occurred and now you need to debug that error. In those scenarios saving error messages in a file will be much helpful. In this method we are redirecting example.sh script output and its error messages to file.log.

[root@localhost ~]# ./example.sh > file.log 2>&1

 

 

 

 

 

 

Popular Recommendations:-

How to Defragment an XFS Filesystem in Linux(5 Simple and Effective Steps)

How to Install jq(JSON Processor) on RHEL/CentOS 7/8 

How to Install Arpwatch tool on RHEL/CentOS 7/8(Simple and Effective Steps)

How to Install and Configure Squid Proxy Server on RHEL/CentOS 7/8

Python3: ModuleNotFoundError: No Module Named “prettytable” in Linux 

How to List all the Installed Python Modules in Linux{2 Easy Methods}

Solved: ModuleNotFoundError: No Module Named “requests” in Python 3

How to Install and Enable EPEL Repository on RHEL/CentOS 7/8{Simple and Easy Steps}

Leave a Comment