Mini-Class: Shell Scripting, Day 3

In the script you wrote during yesterday’s lesson, you assigned the value of “shell scripting” to the variable named “SKILL”:

SKILL="shell scripting"

We didn’t cover the rules around naming variables, so let’s do that now.

Valid Variable Names

Variable names can contain letters, digits, and underscores. However, variables can only start with letters or underscores. They cannot start with a digit. Here are examples of valid variable names:

SKILL
SKILL1
_SKILL

Here are examples of an invalid variable names:

3SKILL
A-SKILL
E@MAIL

By convention (not by rule or syntax), variable names are in all uppercase. This way you can quickly spot variables in your scripts. You can use lowercase or even mixed case variable names:

skill
Skill
ASkill

Variable Expansion

When you reference a variable by name, you get its value back. To do that, you have to enclose the variable name in curly braces and precede it with a dollar sign. When you want the value of “SKILL” use “${SKILL}”.

echo "I want to be good at ${SKILL}.  That's why I practice ${SKILL}."

Another less strict way to use variables is by preceding them with a dollar sign and omitting the braces like this:

echo "I want to be good at $SKILL.  That's why I practice $SKILL."

Sometimes shell scripters are lazy and use $VARIABLE instead of ${VARIABLE}.  However, if you want to immediately precede or follow the variable with data you have to use the ${VARIABLE} syntax.  If you don’t, bash doesn’t know where the variable name starts and ends.

Scripting Time

Let’s create a script to demonstrate when the ${VARIABLE} syntax is required.

$ nano day3.sh

Type the following contents into the file. Be very careful to use the same spacing, case, and punctuation as shown below:

#!/bin/bash
WORD="script"
echo "${WORD}ing is fun!"

Give your script execute permissions and run it:

$ chmod +x day3.sh
$ ./day3.sh

You should see the following text on your screen:

scripting is fun!

By using “${WORD}ing”, bash knows you are referencing the variable named “WORD” and that “ing” is not part of the variable name.  If you were to do the following, you would not get the desired result:

echo "$WORDing is fun!"

That would cause bash to return the value for the “WORDing” variable which doesn’t exist!  Because the variable doesn’t exist, nothing is substituted for it and you would see this output:

 is fun!

If you get into the habit of always using the ${VARIABLE} syntax, you won’t run into these types of issues.  However, you’ll see other scripters (who are not as good as you) use the $VARIABLE syntax.

Capitalization Matters

Trying to access a variable named “WORDing” reminded me of an important point you need to be aware of: capitalization matters.  These are three completely different variables:

WORD
Word
word

If you assign a value to a variable named “WORD”, you cannot access its value by using “${Word}” or “${word}”.  The case has to match.  This is another reason to conform to the convention of using capitalized variable names.

When you learn and apply best-practices you save yourself from wasted time and frustration in the future.

Variable Reassignment

Let’s add two more lines to the “day3.sh” script.

$ nano day3.sh

Add the following two lines to the bottom of your script:

WORD="read"
echo "${WORD}ing is fun!"

Your entire script should now look like the following:

#!/bin/bash
WORD="script"
echo "${WORD}ing is fun!"
WORD="read"
echo "${WORD}ing is fun!"

Execute it:

$ ./day3.sh

You should see the following text on your screen:

scripting is fun!
reading is fun!

Congratulations!  You just performed variable reassignment.  😉

If you want to change the value of a variable, simply create another assignment statement.  Just remember that scripts are read and executed from top to the bottom.

Commenting Your Scripts

As you start to write longer and more complicated scripts, you’ll want to use comments. If you need to update one of your scripts a year after your initial wrote it, having a few notes in the script really helps. The same is true for someone who is looking at your script for the very first time.

To create a comment, start a line with a pound sign.  Comments are not executed. They are just simply there for us humans.  The shebang is the only exception. In all other cases the information following a pound sign is there just for humans.  Anything you put after the pound sign will not affect the functioning of your shell script.

I recommend that you write a comment at the top of your script stating the goal of the script or what happens when the script is executed.  For example, we could use this as the header or opening comment for the script we’re working on today:

# This script is used for variable practice.

You can also use comments to describe what a command does or what a series of commands do.  For example, you might use this comment before the first variable assignment:

# Assign a value to a variable.

You might also use this comment before the variable reassignment:

# Change the value stored in the WORD variable.  (Reassignment.)

Comments act as a shortcut to determining what the script does.  They are inline documentation, if you will. If the commands are relatively simple and self-explanatory, then you can skip commenting them.

Using blank lines can make your scripts more readable. They can be used to visually separate logical blocks of code. Like comments, blank lines do not change the operation of your script.

Let’s add a blank line to visually separate the two logical code blocks in our script. Here’s the original script:

#!/bin/bash
WORD="script"
echo "${WORD}ing is fun!"
WORD="read"
echo "${WORD}ing is fun!"

Here is the updated script with comments and blank lines:

#!/bin/bash

# This script is used for variable practice.

# Assign a value to a variable.
WORD="script"
echo "${WORD}ing is fun!"

# Change the value stored in the WORD variable.  (Reassignment.)
WORD="read"
echo "${WORD}ing is fun!"

What You’ve Learned Today

In today’s lesson you learned the rules for naming variables.  You also learned that capitalization is important.  Finally, you learned how to use comments and blank lines to make your scripts easy to read and understand.

Here’s What To Expect Next…

In tomorrow’s lesson you’ll learn a simple way to make writing long and complex shell scripts a breeze.

All the best,

Jason

P.S. If you’re ready to master shell scripting once and for all check this out.