|
|
3.2. Variables3.2.1. Types of variablesAs seen in the examples above, shell variables are in uppercase characters by convention. Bash keeps a list of two types of variables: 3.2.1.1. Global variablesGlobal variables or environment variables are available in all shells. The env or printenv commands can be used to display environment variables. These programs come with the sh-utils package. Below is a typical output:
3.2.1.2. Local variablesLocal variables are only available in the current shell. Using the set built-in command without any options will display a list of all variables (including environment variables) and functions. The output will be sorted according to the current locale and displayed in a reusable format. Below is a diff file made by comparing printenv and set output, after leaving out the functions which are also displayed by the set command:
3.2.1.3. Variables by contentApart from dividing variables in local and global variables, we can also divide them in categories according to the sort of content the variable contains. In this respect, variables come in 4 types:
We'll discuss these types in Chapter 10. For now, we will work with integer and string values for our variables. 3.2.2. Creating variablesVariables are case sensitive and capitalized by default. Giving local variables a lowercase name is a convention which is sometimes applied. However, you are free to use the names you want or to mix cases. Variables can also contain digits, but a name starting with a digit is not allowed:
To set a variable in the shell, use VARNAME="value" Putting spaces around the equal sign will cause errors. It is a good habit to quote content strings when assigning values to variables: this will reduce the chance that you make errors. Some examples using upper and lower cases, numbers and spaces:
3.2.3. Exporting variablesA variable created like the ones in the example above is only available to the current shell. It is a local variable: child processes of the current shell will not be aware of this variable. In order to pass variables to a subshell, we need to export them using the export built-in command. Variables that are exported are referred to as environment variables. Setting and exporting is usually done in one step: export VARNAME="value" A subshell can change variables it inherited from the parent, but the changes made by the child don't affect the parent. This is demonstrated in the example:
When first trying to read the value of full_name in a subshell, it is not there (echo shows a null string). The subshell quits, and full_name is exported in the parent - a variable can be exported after it has been assigned a value. Then a new subshell is started, in which the variable exported from the parent is visible. The variable is changed to hold another name, but the value for this variable in the parent stays the same. 3.2.4. Reserved variables3.2.4.1. Bourne shell reserved variablesBash uses certain shell variables in the same way as the Bourne shell. In some cases, Bash assigns a default value to the variable. The table below gives an overview of these plain shell variables: Table 3-1. Reserved Bourne shell variables
3.2.4.2. Bash reserved variablesThese variables are set or used by Bash, but other shells do not normally treat them specially. Table 3-2. Reserved Bash variables
Check the Bash man, info or doc pages for extended information. Some variables are read-only, some are set automatically and some lose their meaning when set to a different value than the default. 3.2.5. Special parametersThe shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed. Table 3-3. Special bash variables
The positional parameters are the words following the name of a shell script. They are put into the variables $1, $2, $3 and so on. As long as needed, variables are added to an internal array. $# holds the total number of parameters, as is demonstrated with this simple script:
Upon execution one could give any numbers of arguments:
More on evaluating these parameters is in Chapter 7 and Section 9.7. Some examples on the other special parameters:
User franky starts entering the grep command, which results in the assignment of the _ variable. The process ID of his shell is 10662. After putting a job in the background, the ! holds the process ID of the backgrounded job. The shell running is bash. When a mistake is made, ? holds an exit code different from 0 (zero). 3.2.6. Script recycling with variablesApart from making the script more readable, variables will also enable you to faster apply a script in another environment or for another purpose. Consider the following example, a very simple script that makes a backup of franky's home directory to a remote server:
First of all, you are more likely to make errors if you name files and directories manually each time you need them. Secondly, suppose franky wants to give this script to carol, then carol will have to do quite some editing before she can use the script to back up her home directory. The same is true if franky wants to use this script for backing up other directories. For easy recycling, make all files, directories, usernames, servernames etcetera variable. Thus, you only need to edit a value once, without having to go through the entire script to check where a parameter occurs. This is an example:
|
