Jared
Jared

Reputation: 125

When echo command used, shows up 0 instead of nothing

I'm working on a little project and came across (big suprise) another issue. Here is the code that has something to do with the echo command:

@echo off
set /a project=nothing
echo The current project is %project%
pause
exit

When the file is run, it says:

"The current project is 0"

Anyone know what my problem might be?

Upvotes: 2

Views: 96

Answers (1)

paxdiablo
paxdiablo

Reputation: 881523

set /a is for setting numerical values based on expressions, such as:

pax> set /a "pi10k = 39270000 / 1250"
31416

If you just want to set it to a string, use:

set project=nothing

As per the output of set /?:

SET /A expression

The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence:

()                  - grouping
! ~ -               - unary operators
* / %               - arithmetic operators
+ -                 - arithmetic operators
<< >>               - logical shift
&                   - bitwise and
^                   - bitwise exclusive or
|                   - bitwise or
= *= /= %= += -=    - assignment
  &= ^= |= <<= >>=
,                   - expression separator

If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes.

Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.

If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression. The assignment operator requires an environment variable name to the left of the assignment operator. Numeric values are decimal numbers, unless prefixed by 0x for hexadecimal numbers, and 0 for octal numbers. So 0x12 is the same as 18 is the same as 022. Please note that the octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

The bold bit in the text above is the bit that causes it to be set to zero, assuming you have no environment variable called nothing. If you do have such a variable, its value is used instead:

pax> set /a xyzzy=plugh
0
pax> set plugh=42

pax> set /a xyzzy=plugh
42

Upvotes: 3

Related Questions