Changing Variables

In some situations, you need to change the name of a variable. To do this, you need to define a new variable that is based on the value of an old variable. This may be useful to change the argument that a user has entered when starting the script. When changing a variable, you should be aware that you can redefine all the variables, except arguments that were entered when starting the script. So if you need to do something to the value assigned to an argument, put the current value of the argument in a new variable, and change that. The example in Listing 27-10 shows how to put the result of an existing variable in a new variable.

Listing 27-10. Assigning the Value of Existing Variables to New Variables

# Greet the user in a friendly way

# Usage: ./hello, <firstname> <surname>

If, for example, a user named Linda Thomson starts the script by using the ./hello, Linda Thomson command, the script will output "hello, Linda Thomson" to the screen. Now put this way, it is not extremely useful to put the current values of $1 and $2 in a new variable called name; if you want to change the value currently assigned to a variable, however, it can be useful to assign the value of old variables to a temporary used new variable. The next section makes this clear.

Was this article helpful?

0 0

Post a comment