Understanding Scope, Recursion, Return Codes, and Data Sharing

Now that you have a basic understanding of the use and operation of functions in shell scripts, let's look at more advanced topics such as scope, recursion, return codes, and data sharing.


The term scope refers to the region within a program where a variable's value can be accessed. There are two types of scope:

  • Global scope If a variable has global scope, its value can be accessed from anywhere within a script. Variables with global scope are referred to as global variables.

  • Local scope If a variable has local scope, its value can only be accessed within the function in which it is declared. Variables with local scope are referred to as local variables.

By default all variables, ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.