Variables. Sure, you can find a couple of them in your life every day. The fact that your girlfriend shows up at the correct time for the date you arranged last week or if the sun will shine like never before are variables that you know will occur only once you live that moment. However, today we will talk about variables that hold some specific data, and in particular, these variables exist inside the UNIX Shell.
This article will pick up from where I left off last week in the article about the UNIX Shell (titled "The Shell and UNIX"). Furthermore, you can find more articles here or under my profile. These cover the Shell-UNIX programming question. I've given you some interesting information already about the UNIX Shell, so if you missed it I definitely advise you to go back and read my earlier articles. They will provide you with real support in reading this one and extending your Shell programming knowledge.
So what do you need to know about variables inside the UNIX Shell? The Shell uses variables to store data and information. Because the universal language that the designers chose for UNIX is text strings, the value and the variable itself will be also a string. The assignment is simple, as you can see below.
[name of variable]=[the string you assign to the variable]
For example:
Color=red
This will create a variable with the name of Color and with the value of red. The creation of variables is dynamic. They will be created when needed. No definition is required. We can refer to the variable later on by putting a $ sign prior to its name. The shell will then replace the variable with its value on the command line. This why the
echo $color
will echo back the red string. If we refer to a variable that does not exist via the $ syntax, it will be created and initialized with an empty string.
Sometimes we want to add meta-characters to strings. These characters have a special meaning to the shell, and this by default should be replaced by something else, depending on what it is. If this occurs, you can still assign those strings with the help of quotes, as you will see on the following page.