/ Linux Reviews / Beginners: Learn Linux / Bash Scripting Introduction - en 4. VariablesA 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 VariablesExample 9 shows how to assign a string of characters to a variable.
In the above example, the name of the variable is
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. 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:
If there had been no braces around the above variable name, the shell
would have looked for a variable named A common task is to append (or prepend) a string to the current value of a variable. Example 12 shows how to do each. 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
4.3. Built-in VariablesBash 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:
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 |