Search code examples
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:

  • name variable like git to check how it will act with other file names - I have to 2 files which have git in their names but at the beginning git-push and git-commit
  • name variable like merge to check how it will act with unique file name - I have 1 file with name merge-sprint
  • create new file named my-merge-sprint to check how it will act if name will be both at the beginning and middle of file name
  • save previous file and delete merge-sprint file to check how it will act with unique file and if name will be in the middle of file name
  • renamed file to merge-sprint-name to check how it will act if there was name at the end of file name
  • renamed file to my-merge-sprint-name to check how it will act if there was name at the end of file name and the name of variable will be in the middle of file name

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