Alphaneo
Alphaneo

Reputation: 12539

Spaces in batch files

I have a batch file in which I write, which doesn't work

set num_args = 0

but

set num_args=0

works.

If this is a property of batch file, it's fine. Can we somehow override this to make the batch file look more elegant.

Edit: Batch file in windows.

Upvotes: 1

Views: 155

Answers (2)

Helbreder
Helbreder

Reputation: 922

You can define a procedure to do an assignment (somewhere outside of a scope of a main program). Something like this:

:ASSIGN [VarName] [Value]
    call set %~1=%~2
    exit /B 0

Use it like this:

call :ASSIGN num_args 0
call :ASSIGN some_var 1
call :ASSIGN text_var "A text with spaces in it."

Upvotes: 1

MRAB
MRAB

Reputation: 20654

In Windows, those aren't the same.

This:

set num_args = 0

creates a variable called "num_args " with the value " 0" (note the spaces), whereas this:

set num_args=0

creates a variable called "num_args" with the value "0".

Upvotes: 4

Related Questions