batch-file

For what doesn "_name" stands for after varibale name


Prehistory: I discovered it suddenly and just wondered what is it :)

The question is title, so I will just provide some experiments results:

code

@echo off

set a="5"

echo %a%
echo %a_name%
echo %a_something%
echo a: %a%
echo a: %a_name%
echo a: %a_something%

output

"5"
5
ECHO is off.
a: "5"
a: 5
a:

code

@echo off

set b="5"

echo %b%
echo %b_name%
echo %b_something%
echo b: %b%
echo b: %b_name%
echo b: %b_something%

output

"5"
ECHO is off.
ECHO is off.
b: "5"
b:
b:

code

@echo off

set branch="5"

echo %branch%
echo %branch_name%
echo %branch_something%
echo branch: %branch%
echo branch: %branch_name%
echo branch: %branch_something%

output

"5"
(echo get-branch-name)
ECHO is off.
branch: "5"
branch: (echo get-branch-name)
branch:

So from these experiments I have a conclusion that a and branch are some special names because when tried a lot of other words they all acted exactly like b

The third experiment shows the name of one of my bat files get-branch-name and I have no idea why? Because I tried to:

And the output for all these experiments was exactly like for b

If you need more information for example you need the source codes or anything else please ask me and I will provide it


Solution

  • The inconsistent results you are getting are most likely caused by pollution of your cmd-environment. The results for a suggest you have created an environmentvariable a_name with value 5.
    The results for b are as expected, variables b_name and b_something do not exist.
    You can avoid these problems by preventing pollution of the cmd-environment by proper use of setlocal/endlocal.
    In the code below I have added showing all variables starting with letter a and copied the echo lines with the results encapsulated in [] to avoid the echo off messages and show possible leading/trailing spaces.
    Example:

    @echo off
    setlocal
    
    set a="5"
    set a
    echo %a%
    echo %a_name%
    echo %a_something%
    echo a: %a%
    echo a: %a_name%
    echo a: %a_something%
    echo [%a%]
    echo [%a_name%]
    echo [%a_something%]
    echo [a: %a%]
    echo [a: %a_name%]
    echo [a: %a_something%]
    endlocal