Why does this batch variable never change even when set?











up vote
11
down vote

favorite
4












@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)


It seems that the variable "first" is always 0. Why?










share|improve this question


















  • 2




    Possible duplicate of Windows Batch Variables Won't Set
    – phuclv
    Mar 11 at 1:24















up vote
11
down vote

favorite
4












@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)


It seems that the variable "first" is always 0. Why?










share|improve this question


















  • 2




    Possible duplicate of Windows Batch Variables Won't Set
    – phuclv
    Mar 11 at 1:24













up vote
11
down vote

favorite
4









up vote
11
down vote

favorite
4






4





@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)


It seems that the variable "first" is always 0. Why?










share|improve this question













@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)


It seems that the variable "first" is always 0. Why?







windows batch-file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 16 '10 at 17:31









jcao219

1,79221622




1,79221622








  • 2




    Possible duplicate of Windows Batch Variables Won't Set
    – phuclv
    Mar 11 at 1:24














  • 2




    Possible duplicate of Windows Batch Variables Won't Set
    – phuclv
    Mar 11 at 1:24








2




2




Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24




Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24












2 Answers
2






active

oldest

votes

















up vote
23
down vote



accepted










With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.



To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).



If you type set /?, you'll see a much more detailed explanation of this at the end of the output.






share|improve this answer




























    up vote
    -1
    down vote













    It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.






    share|improve this answer








    New contributor




    Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
      – Stephan
      Nov 21 at 19:21











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3949978%2fwhy-does-this-batch-variable-never-change-even-when-set%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    23
    down vote



    accepted










    With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.



    To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).



    If you type set /?, you'll see a much more detailed explanation of this at the end of the output.






    share|improve this answer

























      up vote
      23
      down vote



      accepted










      With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.



      To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).



      If you type set /?, you'll see a much more detailed explanation of this at the end of the output.






      share|improve this answer























        up vote
        23
        down vote



        accepted







        up vote
        23
        down vote



        accepted






        With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.



        To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).



        If you type set /?, you'll see a much more detailed explanation of this at the end of the output.






        share|improve this answer












        With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.



        To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).



        If you type set /?, you'll see a much more detailed explanation of this at the end of the output.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 16 '10 at 17:37









        Michael Madsen

        46.2k66075




        46.2k66075
























            up vote
            -1
            down vote













            It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.






            share|improve this answer








            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
              – Stephan
              Nov 21 at 19:21















            up vote
            -1
            down vote













            It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.






            share|improve this answer








            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
              – Stephan
              Nov 21 at 19:21













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.






            share|improve this answer








            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.







            share|improve this answer








            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 21 at 18:55









            Spy_Prototype_96.1.5-B HD

            11




            11




            New contributor




            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Spy_Prototype_96.1.5-B HD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
              – Stephan
              Nov 21 at 19:21


















            • That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
              – Stephan
              Nov 21 at 19:21
















            That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
            – Stephan
            Nov 21 at 19:21




            That's just wrong. Although I'm interested, which "other options" you're talking about (besides /a and /p)
            – Stephan
            Nov 21 at 19:21


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3949978%2fwhy-does-this-batch-variable-never-change-even-when-set%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu