/ Linux Reviews / Beginners: Learn Linux / Bash Scripting Introduction - en


4. Variables

A variable is a name for a place in memory where a shell script can store a string of characters. Those characters can be anything, including letters, digits, punctuation, spaces and tabs. In the following sections, we discuss how to assign values to variables and how to access those values after they've been assigned.

4.1. Using Variables

Example 9 shows how to assign a string of characters to a variable.

Example 9. Assigning a String to a Variable


CARMODEL=Porsche
            

In the above example, the name of the variable is CARMODEL, and the string that is stored in the variable is Porsche. There must not be any spaces around the '=' character, and the string must be a single word. If the string contains any whitespace (i.e., spaces, TABs, or newlines), then it must quoted, as shown in Example 10.

Example 10. Assigning a String with Whitespace to a Variable


CARMODEL="Mini Cooper"
            

As discussed in previous sections, the quotes cause the string inside the quotes to be taken as a single word (as well as escaping various shell metacharacters). If there were no spaces (and no metacharacters that needed to be escaped) in the string, we wouldn't have needed the quotes, but it's good style to always use quotes when assigning a value to a variable.

A variable's name must start with a letter or an underscore. The following characters can be letters, digits, or underscores. It is convention to write variable names in all uppercase characters, but that's not required. Here are some valid variable names: FIRST_NAME, LINE1, and _FILENAME. Here are some invalid variable names: 3rd_PARTY, MY-VAR, and INTEREST%.

After you assign a string to a variable, you can access that string by writing the variable name prefixed with a '$', as shown in Example 11. This is called variable expansion.

Example 11. Referencing a Variable's Value


echo "The model of the vehicle is: $CARMODEL"
OLDMODEL="$CARMODEL"
            

Notice that double quotes do not escape the meaning of the '$' metacharacter (but single quote will). If you want the variable expansion to be adjacent to other letters or digits, you need to use braces to mark the start and end of the variable name, as follows:


echo "There are 5 ${CARMODEL}s for sale."
CARPLURAL="${CARMODEL}s"
	  

If there had been no braces around the above variable name, the shell would have looked for a variable named CARMODELs instead of CARMODEL, and the script would have malfunctioned, because there is no variable named CARMODELs.

A common task is to append (or prepend) a string to the current value of a variable. Example 12 shows how to do each.

Example 12. Appending and Prepending Text to a Variable's Value


CUSTOMER_NAME="Fred"
CUSTOMER_NAME="$CUSTOMER_NAME Smith"  # Appends a string.
CUSTOMER_NAME="Mr. $CUSTOMER_NAME"    # Prepends a string.
echo "$CUSTOMER_NAME"                 # Output is: Mr. Fred Smith
            

4.2. Kinds of Variables

There are two kinds of variables: shell variables and environment variables. Shell variables are not inherited by child processes, and environment variables are inherited by child processes. Other than that one difference, they behave exactly the same. Why would you want a variable to be inherited by a child process? It's a convenient way to communicate information to a child process. Additionally, there are several common environment variables that many systems define for every user, such as HOME and PATH. Since those are environment variables in the initial login shell, every process descended from that shell inherits those variables.

4.3. Built-in Variables

Bash automatically defines several built-in variables that contain useful information. Whether or not you can assign new values to built-in variables depends on the variable. The Bash manual (http://www.gnu.org/software/bash/manual/bashref.html) lists them all, but here are a few:

BASH_VERSION

This variable expands to the current version of the Bash shell. You cannot assign a value to this variable. This is a shell variable.

HOSTNAME

This variable expands to the hostname of the computer on which the Bash shell is executing. You cannot assign a value to this variable. This is an environment variable.

PWD

This variable expands to the current working directory of the Bash shell (as it was last set by the cd command). You cannot assign a value to this variable. This is an environment variable.

HOME

This variable expands to the pathname of the home directory of the user who is running the shell. You can assign to this variable to change it temporarilly during a single shell session, but the next time you login, it will be set to its original value. This is an environment variable.

There are many more built-in variables. See the Bash man page or the Bash Manual (http://www.gnu.org/software/bash/manual/bashref.html) for details.


/ Linux Reviews / Beginners: Learn Linux / Bash Scripting Introduction


Meet new people

Adult Dating