Variable Odds and Ends! Shell Scripting Part 6

Published on September 16, 2022 at 6:18 pm by LEW

Introduction

In this post we will cover a few odds and ends of variables in bash scripting. Specifically local variables, variable expansion, and variable arrays. With the information presented in this series of posts, you should be able to write some bash shell scripts that can assist you in a variety of tasks.

For an example of what you can do with a bash script; even though there are a lot of prepackaged backup programs out there, this can be one administrative task the minimalist might want to write their own bash script for. If there is interest I will walk though creating such a script.

But for the final post in this series we will focus on some things you may want to know about bash script variables.

Local Variables

It is generally good programing practice to not define all variables as global in scope. In bash scripts, local variables should be defined in the section of code they will be used (this usually means within a function).

When you prefix a variable declaration with the “local” keyword, then the variable has limited scope. Declaring a variable within a function will look like this.

my_func () {
	local MY_VAR=45
}

While this code does not do anything, it demonstrates creating a local variable, MY_VAR, that will only exist within my_func.

Variable Expansion

In bash, variable expansion is similar to substitution. Basically if we declare a variable, say “COUNT=5”, to use that variable we must expand it with the “$” sign. Hence to get the value of count we write $COUNT.

This is part of parameter expansion, which is a topic for another day. In this case I want to point out that the variable can be used in one of two ways. For our COUNT variable we can write/use it in either fo the following two forms.

$COUNT
${COUNT}

For what we are doing in this series, either form is acceptable.

An argument can be made for the ${COUNT} form being more readable in ones code. The primary case for this is when the variable is right up against other text, with no white space. Enclosing the variable in squiggly brackets makes it stand out, and there is less chance of a codding error. In the end, just be consistent throughout your code with how you handle variables.

Variable Arrays

To define and fill a variable array, the bash script statement will look like this.

MY_ARRAY=("Quick" "Brown" "Fox")

To add a value to the array looks like this.

MY_ARRAY+=("Jumped" "Over")

Needs to know how many elements are in the array, that looks like this.

echo "${#MY_ARRAY[@]}"

To access a specific element (remembering that the element count starts with zero) looks like this.

echo "${MY_Array[2]}"

Conclusion

This is the end of my six part series on basic bash scripting. I just want to say we have barely scratched the surface. The BASH scripting langue can be very useful for automating various tasks.

If there is enough interest, I will  create some practical examples of bash scripting for some common administrative tasks (which will probably also include setting up cron jobs to automate said tasks). So let me know.

Shell or Terminal? Shell Scripting – Part 1

Simple Script Example! Shell Scripting – Part 2

Command Line Arguments! Shell Scripting Part 3

User Input! Shell Scripting Part 4

Using Functions! Shell Scripting Part 5

Variable Odds and Ends! Shell Scripting Part 6

Add New Comment

Your email address will not be published. Required fields are marked *