if
Performs conditional processing in batch programs. Windows commands reference
On Windows
Windows
if [not] ERRORLEVEL <number> <command> [else <expression>]if [not] <string1>==<string2> <command> [else <expression>]if [not] exist <filename> <command> [else <expression>]if [/i] <string1> <compareop> <string2> <command> [else <expression>]if cmdextversion <number> <command> [else <expression>]if defined <variable> <command> [else <expression>]if not exist product.dat echo Cannot find data file:begin@echo offformat a: /sif not errorlevel 1 goto endecho An error occurred during formatting.
notSpecifies that the command should be carried out only if the condition is false.errorlevel <number>Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number.<command>Specifies the command that should be carried out if the preceding condition is met.<string1>==<string2>Specifies a true condition only if string1 and string2 are the same. These values can be literal strings or batch variables (for example, %1). You do not need to enclose literal strings in quotation marks.exist <filename>Specifies a true condition if the specified file name exists.<compareop>Specifies a three-letter comparison operator, including:
- EQU - Equal to
- NEQ - Not equal to
- LSS - Less than
- LEQ - Less than or equal to
- GTR - Greater than
- GEQ - Greater than or equal to/iForces string comparisons to ignore case. You can use /i on the string1==string2 form of if. These comparisons are generic, in that if both string1 and string2 are comprised of numeric digits only, the strings are converted to numbers and a numeric comparison is performed.cmdextversion <number>Specifies a true condition only if the internal version number associated with the command extensions feature of Cmd.exe is equal to or greater than the number specified. The first version is 1. It increases by increments of one when significant enhancements are added to the command extensions. The cmdextversion conditional is never true when command extensions are disabled (by default, command extensions are enabled).defined <variable>Specifies a true condition if variable is defined.<expression>Specifies a command-line command and any parameters to be passed to the command in an else clause./?Displays help at the command prompt.Several platforms
Perform conditional processing in shell scripts.
Execute the specified commands if the condition command's exit status is zero
if condition_command; then echo "Condition is true"; fiIdentifyExecute the specified commands if the condition command's exit status is not zero
if ! condition_command; then echo "Condition is true"; fiIdentifyExecute the first specified commands if the condition command's exit status is zero otherwise execute the second specified commands
if condition_command; then echo "Condition is true"; else echo "Condition is false"; fiIdentifyCheck whether a file exists
if [[ -f path/to/file ]]; then echo "Condition is true"; fiIdentifyCheck whether a directory exists
if [[ -d path/to/directory ]]; then echo "Condition is true"; fiIdentifyCheck whether a file or directory exists
if [[ -e path/to/file_or_directory ]]; then echo "Condition is true"; fiIdentifyCheck whether a variable is defined
if [[ -n "$variable" ]]; then echo "Condition is true"; fiIdentifyList all possible conditions (test is an alias to [; both are commonly used with if)
man testIdentifyWindows
Perform conditional processing in batch scripts.
Execute the specified commands if the condition is true
if condition (echo Condition is true)IdentifyExecute the specified commands if the condition is false
if not condition (echo Condition is true)IdentifyExecute the first specified commands if the condition is true otherwise execute the second specified commands
if condition (echo Condition is true) else (echo Condition is false)IdentifyCheck whether %errorlevel% is greater than or equal to the specified exit code
if errorlevel 2 (echo Condition is true)IdentifyCheck whether two strings are equal
if %variable% == string (echo Condition is true)IdentifyCheck whether two strings are equal without respecting letter case
if /i %variable% == string (echo Condition is true)IdentifyCheck whether a file exist
if exist path\to\file (echo Condition is true)Identify