cmd batch nul partial usage
Usage 1:2>nul
2>nul is the information displayed when the blocking operation fails, and it will still be displayed if it is successful.
For example: create a folder named test in md (or mkdir) where the batch file is located. An error will be reported when this folder has been created. Use 2>nul to block this prompt
md test 2>nul
Usage 2: >nul (i.e. 1>nul)
>nul is the information displayed successfully by the blocking operation, but it will still be displayed if there is an error (i.e. 1>nul)
The @ symbol cancels the input display of this line
like:
Example 1: ping an IP address. The input situation will be displayed in cmd first, c:\user\administrator\ping 192.168.1.1 Regardless of whether it can be pinged or not, the interface will generally output 4 consecutive ping lines.
@echo off
ping 192.168.1.1
pause
Example 2: Add @ before the statement, and the input situation will no longer be displayed. Show ping output results directly
@echo off
@ping 192.168.1.1
pause
Example 3: Add >nul at the end of the statement and no longer display the output result. Since @ is added, the input situation will not be displayed.
@echo off
@ping192.168.1.1 >nul
pause
Usage 3: >nul 2>nul (i.e. 1>nul)
Block all output statements to display, no longer prompts whether successful or failed interface
About "1>" "2>" "2>" "2>&1"
The file descriptor for standard input is 0
Standard output is 1
Standard error is 2> and >> are both redirected output (> is overwrite, >> is append)
1> Refers to the standard information output path (that is, the default output method)
2> Refers to the error message output path
2>&1 refers to specifying the standard information output path as the error message output path (that is, they are all output together)
Supplementary Question 4<&0:
< and << are both redirect inputs
<0 means the standard input path
4<&0 refers to specifying file descriptor 4 as standard input (actually optional any number between 4 and 9)
grep "standard"* > 2>&1
Write all lines containing "standard" into the file, including the error message that an error occurred during grep
del >nul 2>nul
This ">nul 2>nul" can block all screen output, which can be understood as follows:
If you execute a command but don't want to see the execution of this command on the screen, you can use "[Command]>nul" to block the output of the command on the screen. However, some commands will have errors in execution. Even if you use ">nul", you cannot block the information generated by the command. Therefore, add "2>nul" to the back, which is "[Command]>nul + space + 2>nul". In this way, no matter whether the command is running correctly, you will not see the screen displayed by this command on the screen.
Use the "dir" command to display the file and folder list of the current directory. If you use "dir>nul", you will not see the screen output of the dir command. Then when you type "dirr" again, you will display " 'dirr' is not an internal command, nor an external command, nor a batch file." At this time, if you use "dirr>nul" to block this error, because " 'dirr' is not an internal command, nor an external command, nor a batch file." This error prompt, if you use "dirr>nul" at this time, you will not see the above error prompt on the screen.
What does ****1>nul 2>nul in batch processing mean?
1>nul means that the correct prompt for the command to run is not displayed.
2>nul does not display error prompts
Together, no correct or wrong display
> is a redirect symbol
nul means empty device
Enter the prompt to the empty device and it will not be displayed
Redirection you can understand as input to
Example:
@taskkill /im /f /t >nul 2>&1
What does it mean when I see ">nul 2>nul" often?
The function is to block out whatever information to be displayed by the previous command, that is, it will not be displayed.
2>nul brief analysis
2 of 2>nul refers to the error output handle, 2>nul means outputting the executed error result to the empty device nul, that is, no error result is output.
For example. Weak/intelligence 2>nul will not display 'weak/intelligence' is not an internal or external command, nor is it a runnable program or batch file.
That is to say >nul means no echo after successful execution, 2>nul means no echo after unsuccessful execution
>nul 2>nul means that no matter whether the command is executed successfully or not, it will not be reprinted.
TASKKILL /F /IM >nul 2>&1 explanation
Redirect the standard output stream to nul and the standard error stream to the standard output stream.
The content output to nul will be discarded directly, so the effect is to discard all output from the program.
2>&1
Indicates redirecting the standard error stream to the standard output stream.
About the redirection output of cmd command 2>&1
mycommand > 2>&1 should be the most classic usage.
The result of the command can be output in the form of "%>", which means file descriptor: 1 is the standard output stdout, and 2 is the standard error stderr. The system defaults to % value is 1, which means "1>", and 1> can be abbreviated as >, which means default is >. The default target of stdout is the terminal, and the default target of stderr is also the terminal. We execute: echo text > in batch processing, and we can see echo text 1> on the screen, which is the reason.
Among them & needs to be used directly in conjunction with redirect symbols.
Application example:
1. Output the result to
net stop myservices >>result 2>&1
2. Hide the program output results
net stop myservices >nul 2>nul
Microsoft's article on redirection: Using command redirection operators
The command input and output streams can be redirected from the default location to another location using the redirect operator. The location of the input or output data stream is called a handle.
The following table lists the available handles.
Handle Number code of the handle Description
STDIN 0
Keyboard input
STDOUT 1
Output to the command prompt window
STDERR 2
Error output to command prompt window
UNDEFINED 3-9
Handles are defined separately by the application, and they are unique to each tool.
The numbers 0 to 9 represent the first 10 handles. You can run the program using commands and redirect any of the first 10 handles of the program. To specify the handle to use, type the number for the handle before the redirect operator. If a handle is not defined, the default < redirect input operator is 0, while the default > redirect output operator is 1. After typing the < or > operator, you must specify the read and write location of the data. You can specify a file name or other existing handle.
To specify a redirect to an existing handle, use the same character followed by the handle number (i.e., the & handle number) to be redirected. For example, the following command can redirect handle 2 (i.e. STDERR) to handle 1 (i.e. STDOUT):
2>&1
The following table lists the operators that can be used to redirect input and output data streams.
Redirection Operator Description
> Write command output to a file or device (such as a printer), rather than a command prompt window or handle.
< Read command input from a file instead of from a keyboard or handle.
>> Add command output to the end of the file without deleting existing information in the file.
>& Writes the output of one handle to the input of another handle.
<& Read input from one handle and write it to another handle output.
| Read the output from one command and write it to the input of another command. Also known as pipe.
By default, command input (i.e., STDIN handle) can be sent from the keyboard, and then command output (i.e., STDOUT handle) is sent to the command prompt window by .
Redirect input (<)
To redirect input to a file or device via the keyboard, use the < operator. For example, to get input from the sort command, type:
sort<
The contents of the sequential list will be displayed in the command prompt window.
The < operator can open a specified file name with read-only access. Therefore, information cannot be written into the file when using this operator. For example, if the program is started with <&2, all attempts to read handle 0 will fail because handle 2 was initially opened in a write-only access mode.
Notice
0 is the default handle for the < redirect input operator.
Redirect output (>)
Almost all commands send output to the command prompt window. Even commands that send output to the drive or printer will display messages and prompts in the command prompt window.
To redirect output from the command prompt window to a file or device, use the > operator. This operator can be used in many commands. For example, to redirect dir output to , type:
dir>
If it does not exist, the file is created. If present, the information in the file will be replaced with the output of the dir command.
To run the netsh routing dump command and then send the output to , type:
netsh routing dump>c:\
The > operator can open a specified file with write-only access. Therefore, the file cannot be read using this operator. For example, if you start the program with the redirect operator >&0, all attempts to write handle 1 will fail because handle 0 was initially opened as read-only access.
Notice
1 is the default handle for > redirecting the output operator.
Copy handle
Redirect operator & can copy output or input from one specified handle to another specified handle. For example, to send dir output to and error output to , type:
dir>c:\ 2>&1
When copying a handle, you can copy all the characteristics of the original state of the handle. For example, if a handle has a read-only access property, all copies of that handle have a read-only access property. A handle with a read-only access property cannot be copied to another handle with a write-only access property.
Redirect input and copy using the & operator
To use the redirect input operator (<) with the copy operator (&), the specified file must already exist. If the input file exists, the file is opened read-only, and the characters contained in the file are sent as input to this command (as if you entered from the keyboard). If a handle is specified, copy the specified handle to an existing handle on the system.
For example, to open as handle 0 input read (i.e. STDIN), type:
<
To open and send the output to the command prompt window (i.e. STDOUT) after the content is sorted, type:
sort<
To find , and then redirect handle 1 (i.e. STDOUT) and handle 2 (i.e. STDERR) to , type:
findfile > 2<&1
To copy a user-defined handle 3 as handle 0 enter read (i.e. STDIN), type:
<&3
Redirect output and copy using the & operator
If the output is redirected to a file and an existing file name is specified, the file is opened in a write-only manner and the file contents are overwritten. If a handle is specified, copy the file to an existing handle.
To copy a user-defined handle 3 to handle 1, type:
>&3
To redirect all output including handle 2 (i.e. STDERR) from the ipconfig command to handle 1 (i.e. STDOUT), and then redirect the output to , type:
>> 2>&1
Append output using >> Redirect operator
To add output from the command to the end of the file without losing any information already in the file, use two consecutive greater than signs (i.e. >>). For example, you can attach a directory list generated by the dir command to a file using the following command:
dir>>
To append the output of the netstat command to the end of , type:
netstat>>
Use Pipeline Operator (|)
The pipeline operator (|) can extract the output of a command (STDOUT by default) and then direct it to the input of another command (STDIN by default). For example, use the following command to classify directories:
dir | sort
In this example, both commands are initiated at the same time, but the sort command then pauses until it receives the output of the dir command. The sort command uses the output of the dir command as input and then sends the output to handle 1 (i.e. STDOUT).
Merge commands with redirect operators
Custom commands can be created by merging filter commands with additional commands and file names. For example, you can use the following command to store the file name containing the "LOG" string:
dir /b | find "log"
The output of the dir command is sent through the find filter command. The file name containing the string "LOG" is stored in the file as a list of file names (for example, , and ).
To use multiple filters in the same command, use a pipeline (|) to separate the filters. For example, the following command will search each directory on the C drive to find the file name containing the "LOG" string, and display one screen at a time in the command prompt window:
dir c:\ /s /b | find "log" | more
Pipeline (|) can be used to direct , so that it sends the dir command output through the find filter command. The find command selects only the file name containing the string "LOG". The more command can display the file name selected by the find command (one screen is displayed at a time in the command prompt window).