From the course: Linux: Bash Shell and Scripts

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solutions: Using local variables in functions, loops, and arithmetic

Solutions: Using local variables in functions, loops, and arithmetic - Linux Tutorial

From the course: Linux: Bash Shell and Scripts

Start my 1-month free trial

Solutions: Using local variables in functions, loops, and arithmetic

(chime) - [Instructor] Let's look at solutions for the challenges. The first challenge was to essentially do a local variable. So here we have our function f and we use the command typeset to make a local. Then we set a equal to one and that will be an a that's private to the function f. Outside of the function we said a equals two and then we call f, and after f returns we print out the value for a. If a were private, then a will still be two. If a were not private, then the function would have changed it to one. So, let's run it. And we see that a is still two. So, setting a to one and f didn't change the global a. That's just what we wanted. Our second challenge has to do with a for loop. We want to use a for loop to loop over all the files in user bin and for each file if it has this string, unable to fork, in it then we want to print out that file name. So, we're going to change directory user bin and that just happens in the script. When the scripts all done running we'll still…

Contents