ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
I am just learning bash programming. I know about scopes in C and Pascal. Is there some trick to bash scopes?
Well I have a loop and within it I calculate some stuff.
After the loop ends I try to print the totals and the variables seem to go back to 0 as soon as the loop ends. I'm sure its something dumb. Please help if you see my mistake. Thanks.
#!/bin/bash
#total.sh
#MB is 1 MB in bytes,
#NF = running total number of files,
#TS = running total of bytes.
A pipeline will cause each command to be executed in a child process from the parent, called a child process. Each process has its own set of variables. You may also see the term subshell. The variables may be used, changed, etc., but the new values of the variables will not be communicated back the parent.
I checked scoban's link above but it didn't address Scope. Did touch on passing variables in and out of the bash environment, but didn't address my loop scope issue.
I also tried changing the variable operations of TS and NF per next suggestion, but got same results. When the loop was done, those two variables went back to zero (assigned at top of program).
If I assign TS and NF an arbitrary value like 20, during the while loop, NF counts from 21 to 32 (12 JPG files in my local dir) but resets to 20 when I print it outside the loop.
I know this has something to do with scope but not sure what dictates it nor how to solve it in this case.
I thought that was what was happening.
I tried just a for loop and declared some variables above the loop, changed the variables in the loop, and printed outside the loop, and of course everything works fine.
BUT
In this case, the pipe that starts this loop, dictates another environment is created which makes all variable operations stay in that evnironment and not be passed back to the calling invironment. Can I set a variable that is accessable to both environments or that can be passed back to the calling environment in BASH?
Thanks
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.