From the course: Linux System Engineer: Bash Shell Scripting for Automation

Unlock the full course today

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

Bash variable scope

Bash variable scope

From the course: Linux System Engineer: Bash Shell Scripting for Automation

Start my 1-month free trial

Bash variable scope

- [Instructor] One of the most common pitfalls to writing Bash Shell Scripts is unknown variable scope. Bash, unlike many other languages, doesn't have tight variable scope by default. You have four levels of variable scope. The first type of variable scope, global environmental variables which the entire OS can see. These variables are set in the system when it starts. Changes to this environment can be made by editing the shell startup script such as the etc profile, etc bashrc, or the Bash_profile and .bashrc files on the users home directory. Be sure to use the export command so the variables are accessible by any sub processes like shells you will start or scripts you will run. The second type of variable scope is variables that are local to a script which can be seen by any function, command or statement in the script. Anytime we create a variable it will be in this category by default. The third type of variable scope is variables that are local to a script and all sub…

Contents