SoFunction
Updated on 2025-03-10

Set /p= command usage detailed explanation

Set command details

There are two commands for echoing information in batch processing, echo and set /p=<nul. Their common point is that they both execute the program.
The difference between the screen output of the message is that echo is the line break output, while set /p=<nul is the line break to recover the output. In this way, everyone may
I don't understand very much, here are two codes for comparison:

Code:

@echo off
echo bathome
echo batman
pause>nul

Code:

@echo off
set /p=bathome<nul
set /p=batman<nul
pause>nul

Run these two pieces of code, and the result of the first paragraph is that the battery is output on the screen in two lines in turn.
and batman, and the result of the second paragraph is that the battery and batman are output in a row in turn. If you are careful, you can still see the light.
The location of the mark is also different. Okay, that's all.

1. The main function of set is to assign value
1、set /p a=promptstring
First display the propstring, then accept the content input by the user, end with Enter, and assign the value to the variable a

2、set /p a=promptstring<
First display the propstring, and then assign the content of the variable a (usually represented as the first line) in the file on the right of the "<" pipeline number from the first character until it encounters the carriage return character.

3、set /p a=promptstring<nul
First display the propstring, then assign the content in the nul on the right side of the "<" pipeline number to the variable a, and end the statement without pressing Enter. Since nul is an empty device, there is no content to be assigned, and the variable a is still undefined.

2. Because the promptstring can be displayed before accepting user input, set can also be used as a display command (when used only as a display command, variable a can be omitted)
1、set /p =promptstring
Show promptstring, then accept the content entered by the user, and end with Enter. If the user presses Enter directly, only the promptstring will be displayed. (Assigning value to an empty variable, the meaning of assignment has been lost, and it is only used for display. The user needs to press the Enter key to end the statement, and it has no practical purpose)

2、set /p =promptstring<
First display the propstring, and then assign the content of the empty variable from the first character in the file on the right of the "<" pipeline number to the empty variable (no practical use)

3、set /p =promptstring<nul
First display the propstring, then assign the content in the nul on the right side of the "<" pipeline number to the empty variable. The user does not need to press Enter to end the statement. In fact, this sentence pattern is often used as the display statement. Since the cursor does not wrap lines after displaying the propstring, this sentence pattern is used a lot in practice. As mentioned on the second floor, there are also cursor backspaces, etc.