subshell

Set a parent shell's variable from a subshell

北城以北 提交于 2019-11-26 09:08:44
问题 How do I set a variable in the parent shell, from a subshell? a=3 (a=4) echo $a 回答1: The whole point of a subshell is that it doesn't affect the calling session. In bash a subshell is a child process, other shells differ but even then a variable setting in a subshell does not affect the caller. By definition. Do you need a subshell? If you just need a group then use braces: a=3 { a=4;} echo $a gives 4 (be careful of the spaces in that one). Alternatively, write the variable value to stdout

Variables getting reset after the while read loop that reads from a pipeline

你离开我真会死。 提交于 2019-11-26 06:46:21
问题 initiate () { read -p \"Location(s) to look for .bsp files in? \" loc find $loc -name \"*.bsp\" | while read do if [ -f \"$loc.bz2\" ] then continue else filcount=$[$filcount+1] bzip $loc fi if [ \"$scan\" == \"1\" ]; then bzipint $loc fi echo $filcount #Correct counting echo $zipcount #Correct counting echo $scacount #Correct counting echo $valid #Equal to 1 done echo $filcount #Reset to 0 echo $zipcount #Reset to 0 echo $scacount #Reset to 0 echo $valid #Still equal to 1 } I\'m writing a