Print out variables from subshell to parent shell

novel

Very new to Bash and pretty confused about local/global variables/subshells. I'm not sure why the modified variables won't print out at the end of the function—I'm trying to print out a final line count and file count at the end of the file, but if I do, it only prints out 0 because they are local variables. Is there any way to print out the modified values?

count=0
files=0
find . -type f | while IFC= read -r file;
do
   let files=files+1
   wc -l $file
   count=$(($count+$(wc -l < $file)))
   echo "total lines $count ; total files $files"
done
echo $files $count
exit 0
terdon

Yes. But it is absolutely non-intuitive. This will work, for instance:

#!/bin/bash
count=0
files=0
while IFS= read -r file;
do
   let files=files+1
   wc -l $file
   count=$(($count+$(wc -l < $file)))
   echo "total lines $count ; total files $files"
done < <(find . -type f )
echo "$files $count"
exit 0

The <(command) construct is called "process substitution" and lets you treat the output of a command as a "file". Feeding it into the loop this way makes your script work as you expect.

The problem is your use of the pipe (|) which causes the while loop to run in a separate subshell which can't modify variables outside it.

In shells that don't support the <() feature, you can run the command(s) on the right of the pipe in a subsell and include the final echo in that subshell:

#!/bin/bash
files=0
find . -type f | {
    while IFC= read -r file;
    do
        let files=files+1
        wc -l $file
        count=$(($count+$(wc -l < $file)))
        echo "total lines $count ; total files $files"
    done
    echo "$files $count"
}

exit 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a variable from a subshell available in the parent shell

From Dev

exit shell script from a subshell

From Dev

shell variable not exported from subshell?

From Dev

Is it possible for a subshell to execute something in the parent shell?

From Dev

Fish shell input redirection from subshell output

From Dev

how to use variable from subshell in shell

From Dev

Is it possible to print out the shell expansion?

From Dev

Use read builtin command to read from parent stdin while in a subshell

From Dev

IntelliJ - print out variables for debugging?

From Dev

midnight commander subshell - sharing a history file with the shell mc was started from

From Dev

Identifying shell script's subshell number from within a script itself

From Dev

How to get variables from subshell while also passing arguments with xargs?

From Dev

Print out from Servlet

From Dev

How to set variables in a subshell?

From Dev

How do I get an interactive Bash subshell with exactly the same current environment as the parent shell?

From Dev

Switch to parent shell without logging out

From Dev

Print out all remaining variables in awk

From Dev

Print out onto same line with ":" separating variables

From Dev

PhpStorm - print out methods and variables in class files

From Dev

print out list of the relation between variables in python?

From Dev

regarding print out os.environ variables

From Dev

How to set environment variables of parent shell in Python?

From Dev

Displaying output of subshell in parent process

From Java

Print out the numbers from the for loop

From Dev

ruby: Give out a shell script with variables

From Dev

How to find out the user of parent shell inside a child shell?

From Dev

Does shell maths run in a subshell?

From Dev

Determine parent shell from perl

From Dev

Scope of variables executing functions in a subshell

Related Related

  1. 1

    How to make a variable from a subshell available in the parent shell

  2. 2

    exit shell script from a subshell

  3. 3

    shell variable not exported from subshell?

  4. 4

    Is it possible for a subshell to execute something in the parent shell?

  5. 5

    Fish shell input redirection from subshell output

  6. 6

    how to use variable from subshell in shell

  7. 7

    Is it possible to print out the shell expansion?

  8. 8

    Use read builtin command to read from parent stdin while in a subshell

  9. 9

    IntelliJ - print out variables for debugging?

  10. 10

    midnight commander subshell - sharing a history file with the shell mc was started from

  11. 11

    Identifying shell script's subshell number from within a script itself

  12. 12

    How to get variables from subshell while also passing arguments with xargs?

  13. 13

    Print out from Servlet

  14. 14

    How to set variables in a subshell?

  15. 15

    How do I get an interactive Bash subshell with exactly the same current environment as the parent shell?

  16. 16

    Switch to parent shell without logging out

  17. 17

    Print out all remaining variables in awk

  18. 18

    Print out onto same line with ":" separating variables

  19. 19

    PhpStorm - print out methods and variables in class files

  20. 20

    print out list of the relation between variables in python?

  21. 21

    regarding print out os.environ variables

  22. 22

    How to set environment variables of parent shell in Python?

  23. 23

    Displaying output of subshell in parent process

  24. 24

    Print out the numbers from the for loop

  25. 25

    ruby: Give out a shell script with variables

  26. 26

    How to find out the user of parent shell inside a child shell?

  27. 27

    Does shell maths run in a subshell?

  28. 28

    Determine parent shell from perl

  29. 29

    Scope of variables executing functions in a subshell

HotTag

Archive