How to Suppress Output of a Command with NUL in Batch Script
A key part of writing a clean, professional batch script is controlling what the user sees. Many commands produce output that is useful for interactive use but is just "noise" in an automated script. For example, you don't need to see the "1 file(s) copied" message every time your script backs up a file. To create a silent or "quiet" command, you need to redirect its output so that it doesn't appear on the screen.
This guide will teach you how to use the special NUL device to completely suppress the output of any command. You will learn the difference between suppressing normal output and error messages, and the standard syntax for making a command run completely silently.
The Core Concept: Output Streams and the NUL Device
Every command-line program has two primary output streams:
- Standard Output (stdout): This is for normal, successful results (e.g., the file list from
DIR). Its internal handle number is1. - Standard Error (stderr): This is for error messages (e.g., "File Not Found"). Its internal handle number is
2.
The NUL device is a special, virtual "black hole" in Windows. Any data that is sent to NUL is instantly and permanently discarded. By redirecting a command's output stream to NUL, we can effectively silence it.
Suppressing Standard Output (> NUL)
This is the most common form of output suppression. It hides the normal, non-error messages that a command produces. The > operator redirects standard output (stream 1).
Syntax: command > NUL
For example, this script copies a file. The COPY command normally prints a "1 file(s) copied." message.
@ECHO OFF
ECHO --- Running a copy command ---
ECHO.
ECHO First, the normal, noisy version:
COPY "source.txt" "destination.txt"
ECHO.
ECHO Now, the silent version:
COPY "source.txt" "destination2.txt" > NUL
ECHO --- Script finished ---
Output:
--- Running a copy command ---
First, the normal, noisy version:
1 file(s) copied.
Now, the silent version:
--- Script finished ---
The second COPY command ran completely silently.
Suppressing Standard Error (2> NUL)
Sometimes you only want to hide the error messages, not the success messages. This is done by explicitly redirecting stream 2 (stderr).
Syntax: command 2> NUL
For example, this script attempts to delete two files. The first one exists, the second does not.
@ECHO OFF
REM This command will produce an error, which we will suppress.
DEL non_existent_file.txt 2> NUL
Without 2> NUL, this would print "Could Not Find C:...\non_existent_file.txt". With the redirection, it fails silently, which is perfect for cleanup scripts where you don't care if the file was already gone.
The Ultimate Silence: Suppressing All Output (> NUL 2>&1)
This is the standard, professional idiom for making a command completely silent, hiding both its success output and any potential error messages.
Syntax: command > NUL 2>&1
How it works:
> NUL: This redirects Standard Output (stream 1) to theNULdevice.2>&1: This redirects Standard Error (stream 2) to the same location as stream 1. Since stream 1 is already going toNUL, stream 2 is also sent toNUL.
This pattern ensures that absolutely no text from the command will appear on the console.
For example:
@ECHO OFF
ECHO --- Running a PING test silently ---
ECHO You will not see the PING output or any errors.
PING -n 1 non-existent-host.com > NUL 2>&1
REM The command runs silently, but it still sets the ERRORLEVEL.
IF %ERRORLEVEL% NEQ 0 (
ECHO The PING command failed, as expected.
)
Common Pitfalls and How to Solve Them
-
Forgetting to Suppress Errors: A common mistake is to only use
> NULand forget about2> NUL. The script then appears silent during normal operation, but suddenly produces an ugly error message when a command fails.- Solution: For commands that you want to be truly silent, always use the full
> NUL 2>&1redirection.
- Solution: For commands that you want to be truly silent, always use the full
-
Redirection Order: The order matters.
> NUL 2>&1is correct.2>&1 > NULis not the same and will not work as expected (it will redirect errors to the console).- Solution: Always use the
> [destination] 2>&1order.
- Solution: Always use the
-
Losing Important Information: Be careful not to suppress the output of commands when you actually need it for debugging.
- Solution: During development, it's often a good idea to "comment out" the redirection (
REM > NUL) to see what your commands are doing. Add it back in when you are confident the script is working correctly.
- Solution: During development, it's often a good idea to "comment out" the redirection (
Practical Example: A Silent File Copy Operation
This script checks for the existence of a configuration file on a network share and copies it locally if it's missing. The DIR and COPY commands are silenced to provide a clean user experience.
@ECHO OFF
SETLOCAL
SET "SourceConfig=\\FileServer\Config\master.ini"
SET "LocalConfig=C:\ProgramData\MyApp\config.ini"
ECHO --- Application Setup ---
ECHO Checking for local configuration...
REM Check for the local file silently.
DIR "%LocalConfig%" > NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO Local configuration is already in place.
) ELSE (
ECHO Local configuration is missing. Copying from network...
REM Copy the file silently.
COPY "%SourceConfig%" "%LocalConfig%" > NUL
ECHO Copy complete.
)
ENDLOCAL
Conclusion
Suppressing command output is an essential technique for writing clean, user-friendly, and professional batch scripts. The NUL device is your tool for discarding unwanted text.
Key takeaways:
- To suppress normal output, use
> NUL. - To suppress error messages, use
2> NUL. - To make a command completely silent, use the standard idiom
> NUL 2>&1. - Suppressing output does not affect the command's ability to set the
%ERRORLEVEL%, so you can still check for success or failure.