Home > Net >  Evaluate batch script argument
Evaluate batch script argument

Time:04-08

In python, you can evaluate strings like this:

eval("true")

which returns a boolean set to True

Is there a way to pass an entire command into a batch script argument like this: my_script.bat "my_command -do_x=5 do_y=56 ...etc"

Then have the batch script set that argument in the quotes to a variable: set execute_script=%1

And finally run it? In python we would do: eval(execute_script)

How can I do this in batch script?

CodePudding user response:

set "execute_script=%1"
%execute_script%

probably.

CodePudding user response:

The eval function in python is very useful indeed, but it has signatures that defines the eval function to perform actual evaluations dynamically. That is sadly not something cmd has by default. What we can do is to evaluate if a variable is defined, which is not of much use comapred to eval.

So creating a variable, evaluating that it is defined then executing it as a macro:

@echo off
set "string=%*"
if defined string %string%

but that is useless if you simplify it by not creating macros:

if not "%1" == "" %*
  • Related