userpal
userpal

Reputation: 1523

Why does this command fail to print the correct exit code of a Python command?

In a cmd.exe window, when I run python -c "exit(12)" and then run echo %errorlevel%, it prints 12. After that, when I run python -c "exit(13)" & echo %errorlevel%, it prints 12.

Why does the second command fail to print the correct exit code?

Upvotes: 1

Views: 192

Answers (1)

Jeremy Allen
Jeremy Allen

Reputation: 6614

The substitution of %errorlevel% happens when you run the command. It reflects the value of %errorlevel% when you pressed your 'enter' key.

As an example consider:

> set foo=foo

> echo %foo%
foo

> set foo=bar & echo %foo%
foo

> echo %foo%
bar

You can check the errorlevel like this however:

> python -c 'exit(13)' & if errorlevel 13 echo "its 13"

Upvotes: 3

Related Questions