Modify global variable in while loop [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite
3













This question already has an answer here:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

    4 answers



I have a script that process a folder, and count the files in the mean time.



i=1
find tmp -type f | while read x
do
i=$(($i + 1))
echo $i
done
echo $i


However, $i is always 1, how do I resolve this?







share|improve this question












marked as duplicate by muru, Stephen Rauch, peterh, G-Man, taliezin Nov 6 '17 at 7:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Also see: A variable modified inside a while loop is not remembered on Stack Overflow
    – muru
    Nov 6 '17 at 4:10















up vote
10
down vote

favorite
3













This question already has an answer here:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

    4 answers



I have a script that process a folder, and count the files in the mean time.



i=1
find tmp -type f | while read x
do
i=$(($i + 1))
echo $i
done
echo $i


However, $i is always 1, how do I resolve this?







share|improve this question












marked as duplicate by muru, Stephen Rauch, peterh, G-Man, taliezin Nov 6 '17 at 7:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Also see: A variable modified inside a while loop is not remembered on Stack Overflow
    – muru
    Nov 6 '17 at 4:10













up vote
10
down vote

favorite
3









up vote
10
down vote

favorite
3






3






This question already has an answer here:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

    4 answers



I have a script that process a folder, and count the files in the mean time.



i=1
find tmp -type f | while read x
do
i=$(($i + 1))
echo $i
done
echo $i


However, $i is always 1, how do I resolve this?







share|improve this question













This question already has an answer here:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

    4 answers



I have a script that process a folder, and count the files in the mean time.



i=1
find tmp -type f | while read x
do
i=$(($i + 1))
echo $i
done
echo $i


However, $i is always 1, how do I resolve this?





This question already has an answer here:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

    4 answers









share|improve this question











share|improve this question




share|improve this question










asked Nov 6 '17 at 2:52









daisy

27.5k46159290




27.5k46159290




marked as duplicate by muru, Stephen Rauch, peterh, G-Man, taliezin Nov 6 '17 at 7:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by muru, Stephen Rauch, peterh, G-Man, taliezin Nov 6 '17 at 7:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Also see: A variable modified inside a while loop is not remembered on Stack Overflow
    – muru
    Nov 6 '17 at 4:10

















  • Also see: A variable modified inside a while loop is not remembered on Stack Overflow
    – muru
    Nov 6 '17 at 4:10
















Also see: A variable modified inside a while loop is not remembered on Stack Overflow
– muru
Nov 6 '17 at 4:10





Also see: A variable modified inside a while loop is not remembered on Stack Overflow
– muru
Nov 6 '17 at 4:10











2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because you're using the loop with a pipe, which automatically causes it to run in a subshell.



Here is an alternative solution using a while loop:



i=1
while read x; do
i=$(($i + 1))
echo $i
done <<<$(find tmp -type f)
echo $i


And here is the same approach using a for-loop:



i=1
for x in $(find tmp -type f);
do
i=$(($i + 1))
echo $i
done
echo $i


For more information see the following posts:



  • A variable modified inside a while loop is not remembered


  • Bash Script: While-Loop Subshell Dilemma


Also look at the following chapter from the Advanced Bash Scripting Guide:



  • Chapter 23. Process Substitution





share|improve this answer






















  • regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
    – Alexander Mills
    Jun 22 at 3:47

















up vote
1
down vote













#!/bin/bash
i=1
while read x
do
i=$((i+1))
echo $i
done < <(find . -type f)
echo $i


https://stackoverflow.com/questions/7390497/bash-propagate-value-of-variable-to-outside-of-the-loop






share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because you're using the loop with a pipe, which automatically causes it to run in a subshell.



    Here is an alternative solution using a while loop:



    i=1
    while read x; do
    i=$(($i + 1))
    echo $i
    done <<<$(find tmp -type f)
    echo $i


    And here is the same approach using a for-loop:



    i=1
    for x in $(find tmp -type f);
    do
    i=$(($i + 1))
    echo $i
    done
    echo $i


    For more information see the following posts:



    • A variable modified inside a while loop is not remembered


    • Bash Script: While-Loop Subshell Dilemma


    Also look at the following chapter from the Advanced Bash Scripting Guide:



    • Chapter 23. Process Substitution





    share|improve this answer






















    • regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
      – Alexander Mills
      Jun 22 at 3:47














    up vote
    4
    down vote



    accepted










    In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because you're using the loop with a pipe, which automatically causes it to run in a subshell.



    Here is an alternative solution using a while loop:



    i=1
    while read x; do
    i=$(($i + 1))
    echo $i
    done <<<$(find tmp -type f)
    echo $i


    And here is the same approach using a for-loop:



    i=1
    for x in $(find tmp -type f);
    do
    i=$(($i + 1))
    echo $i
    done
    echo $i


    For more information see the following posts:



    • A variable modified inside a while loop is not remembered


    • Bash Script: While-Loop Subshell Dilemma


    Also look at the following chapter from the Advanced Bash Scripting Guide:



    • Chapter 23. Process Substitution





    share|improve this answer






















    • regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
      – Alexander Mills
      Jun 22 at 3:47












    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because you're using the loop with a pipe, which automatically causes it to run in a subshell.



    Here is an alternative solution using a while loop:



    i=1
    while read x; do
    i=$(($i + 1))
    echo $i
    done <<<$(find tmp -type f)
    echo $i


    And here is the same approach using a for-loop:



    i=1
    for x in $(find tmp -type f);
    do
    i=$(($i + 1))
    echo $i
    done
    echo $i


    For more information see the following posts:



    • A variable modified inside a while loop is not remembered


    • Bash Script: While-Loop Subshell Dilemma


    Also look at the following chapter from the Advanced Bash Scripting Guide:



    • Chapter 23. Process Substitution





    share|improve this answer














    In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because you're using the loop with a pipe, which automatically causes it to run in a subshell.



    Here is an alternative solution using a while loop:



    i=1
    while read x; do
    i=$(($i + 1))
    echo $i
    done <<<$(find tmp -type f)
    echo $i


    And here is the same approach using a for-loop:



    i=1
    for x in $(find tmp -type f);
    do
    i=$(($i + 1))
    echo $i
    done
    echo $i


    For more information see the following posts:



    • A variable modified inside a while loop is not remembered


    • Bash Script: While-Loop Subshell Dilemma


    Also look at the following chapter from the Advanced Bash Scripting Guide:



    • Chapter 23. Process Substitution






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 6 '17 at 3:17

























    answered Nov 6 '17 at 3:05









    igal

    4,830930




    4,830930











    • regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
      – Alexander Mills
      Jun 22 at 3:47
















    • regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
      – Alexander Mills
      Jun 22 at 3:47















    regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
    – Alexander Mills
    Jun 22 at 3:47




    regarding the first solution, isn't it process substitution like so < <(find ...) instead of like so <<<(find ...) .... yours is the latter and that seems incorrect.
    – Alexander Mills
    Jun 22 at 3:47












    up vote
    1
    down vote













    #!/bin/bash
    i=1
    while read x
    do
    i=$((i+1))
    echo $i
    done < <(find . -type f)
    echo $i


    https://stackoverflow.com/questions/7390497/bash-propagate-value-of-variable-to-outside-of-the-loop






    share|improve this answer
























      up vote
      1
      down vote













      #!/bin/bash
      i=1
      while read x
      do
      i=$((i+1))
      echo $i
      done < <(find . -type f)
      echo $i


      https://stackoverflow.com/questions/7390497/bash-propagate-value-of-variable-to-outside-of-the-loop






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        #!/bin/bash
        i=1
        while read x
        do
        i=$((i+1))
        echo $i
        done < <(find . -type f)
        echo $i


        https://stackoverflow.com/questions/7390497/bash-propagate-value-of-variable-to-outside-of-the-loop






        share|improve this answer












        #!/bin/bash
        i=1
        while read x
        do
        i=$((i+1))
        echo $i
        done < <(find . -type f)
        echo $i


        https://stackoverflow.com/questions/7390497/bash-propagate-value-of-variable-to-outside-of-the-loop







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 6 '17 at 3:11









        Kamaraj

        2,7001312




        2,7001312












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?