|Lesson 7|| Using strings in variables|
|Objective ||Assign text strings to variables and manipulate them. |
Manipulate Text Strings stored in Variables
Most programming languages have many functions to manipulate text strings stored in variables.
Shell scripts do not. Strings stored in shell script variables are generally used for holding text that does not change and that does not need to be analyzed closely.
You can easily do the following things with a string stored in a shell script variable:
- Assign a string to a variable
- Test the value of a string variable
- Join two strings together
- Print a string
Doing the following tasks with strings is possible, but requires using
additional UNIX commands
and is often a challenge.
If your program requires many string manipulation tasks like those listed here, a shell script is likely not the best choice as a programming environment. A language like Perl provides functions to complete these tasks more easily than a shell script:
- Extract a portion of text from the middle of a string
- Make a string all uppercase or all lowercase
- Update a pattern within a string
Assigning String to Variable
Strings are assigned to variables using the standard format that you are becoming familiar with.
If you need to assign the string “Welcome to our Web site.” to a variable called WELCOME, use this command:
WELCOME="Welcome to our Web site,"
You do not need to specify a length for the variable, or define it as a string variable, as you would in most programming languages.
Printing a string variable is done with the echo command, as you have already seen in a previous lesson.
If the WELCOME variable is assigned a string value as just shown, the following command:
will send the string value to STDOUT (probably printing it on the screen):
Welcome to our Web site,
When you use the dollar sign notation, the value of the string is simply substituted at that point. This means that you can create a combination of strings by using the individual variables. For example, suppose you have defined a variable named WELCOME with the value of “Welcome to our site,” and a variable named NAME with the value of “Nicholas”.
You want to create a third variable whose value combines the two strings and adds a period at the end of the second one.
This would be done with this command:
Printing this variable using the
echo command shows the following value:
Welcome to our site, Nicholas.
Notice a few things about the assignment of the HELLO_MSG variable:
- A space is included between the WELCOME and NAME variables, so a space appears after the comma in the value of HELLO_MSG.
- The entire variable value is enclosed in quotation marks to define it as a single parameter for the shell, thus avoiding any potential ambiguities.
- The NAME variable is followed immediately by a period, so it must be enclosed in braces to identify NAME as the variable’s name. Without the braces, the shell assumes that the variable name ends with the next space character, so it looks for a variable called
NAME, which is not a variable we have defined.
- The space and period are literal text added to the value of HELLO_MSG with the existing WELCOME and NAME variables. You can also add other text, as long as the variable names can be identified by the shell.
In a later module, you will learn how to test the values of variables containing strings.
The next lesson describes how to use numbers in your variables.