How to hide error output in Bash on Linux

I am running following two commands

wp core check-update --path="/var/www/html" 
wp plugin status --path="/var/www/html" 

It always give PHP notice like as follows:

PHP Notice:  Constant ABSPATH already defined in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1231) : eval()'d code on line 60

Is there any way to hide PHP notice message on Bash shell? How can I silence and suppress error messages output in a Bash script from wp command?`

Send output to /dev/null

How to silence output in a Bash script

command 2>/dev/null
command option1 option2 2>/dev/null

###################################
# NOTE: older sh syntax works too #
###################################
command 2>&1  

So update your Linux commands to hide error output in Bash:

wp core check-update --path="/var/www/html"  2>/dev/null
wp plugin status --path="/var/www/html" 2>/dev/null
# Older syntax example 
wp plugin status --path="/var/www/html" 2>1
  • The 2 is stderr (error device with file descriptor # 2 )
  • The 1 is stdout (output device with file descriptor # 1)
  • The /dev/null the null device is a device file that discards all data written to it.
  • So when you type wp plugin status --path="/var/www/html" 2>1, it means redirect the stderr to the file descriptor 1.
  • Also when you type wp plugin status --path="/var/www/html" 2>/dev/null, means redirect stderr to /dev/null file.

A note about shell pipe

Add shell pipe after the 2>/dev/null. Hence,

wp core check-update --path="/var/www/html"  2>/dev/null | command
wp plugin status --path="/var/www/html" 2>/dev/null | command option1
wp plugin status --path="/var/www/html" 2>/dev/null | grep 'foo'

Sending output (stdout) to output.txt, error (stderr) to error.log file

command > output.txt 2> error.log
wp plugin status --path="/var/www/html" > output.txt 2> error.log

# Appending
wp plugin status --path="/var/www/html" >> output.txt 2>> error.log

How to save output but hide errors with Bash

command > output.txt 2> /dev/null
wp plugin status --path="/var/www/html" > output.txt 2> /dev/null

How send both output (stdout) and error (stderr) to log.txt file

command &> log.txt 

# Older syntax from sh
command > out.log 2>&1 
1 Like

Also check our tutorial section:

And:

And:
https://bash.cyberciti.biz/guide//dev/null_discards_unwanted_output


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki