@echo off
set test=%1
if "%1"=="" (
set test=default
echo %test%
) else (
set test=%1
echo %test%
)
Test:
test.bat
Output:
ECHO is off
Problem: The ‘default’ string cannot be assigned as a value to variable ‘test’. I’ve found out that test string’s value is still %1 which has no value
It’s the standard batch beginner bug.
Percent expansion doesn’t work in parenthesis as you expected.
It expands when the complete block is parsed, before any of the lines are executed.
So
%test%is expanded to nothing, the value before it enters the block.The solution is to use delayed expansion here, as
!test!will be expanded at execution not parse time.