Search⌘ K
AI Features

Scope: Local Variables

Explore the concept of local variables in Bash programming, including how they are declared, their scope within the current shell instance, and their behavior in subshells and child processes. Understand how local variables differ from global variables and how changes in subshells do not affect the parent shell. This lesson helps you grasp variable scope to write more reliable Bash scripts.

We'll cover the following...

Local variables

We considered user-defined variables. We can declare them in several ways. Depending on our choice, the new variable comes to the local scope or global scope (environment).

There are two ways to declare the global scope variable:

  1. Add the export command to the variable declaration.
  2. Pass the variable to the program when launching it. We can do it with the env utility when using a shell other than Bash.

If we do not apply any of these ways, our variable comes to the local scope. A variable of this kind is called a local variable and is available in the current instance of the interpreter. A child process (except a ...