NEXT UP previous
Next: Shell Script Parameters

Variables

In common with most programming languages, the shell provides facilities for declaring and using variables. To the shell, a variable is just a place in memory which has a name, and which can hold a string of characters as its value. As soon as you receive a shell prompt, after a login to the system, the shell will already have quite a few variables defined whose values were set up when the shell began execution.

The name and purpose of some of these variables is:

The simplest way to examine the contents of a shell variable is to use the echo command. This command just displays any extra command line parameters you give to it back to the standard output:

	$ echo three extra parameters
	three extra parameters
	$ echo UID
	UID

Obviously, since using echo on the name of a shell variable (UID in the previous example) just displays the name of the variable and not its contents, there must be some other way to access the contents.

Whenever you wish to extract the value from a shell variable you need to precede its name with a dollar ($) symbol. The dollar is a special symbol whose meaning is interpreted by the shell. This also means that if you need to pass a dollar symbol as a parameter to another command, it will need to be quoted. Note that only the backslash and single-quote mechanisms for quoting will work on a dollar symbol. If you need a reminder about the shell quoting mechanisms, refer back to Pathname Expansion in the Bash tutorial. The value of the UID variable can be displayed as follows:

	$ echo the value of UID is $UID 
	the value of UID is 500

The echo command is very useful in shell scripts, because of its ability to display text messages and the values of shell variables.

A shell variable can have a new value assigned to it with the syntax:

	variable=value

The following sequence changes the value of PS1 and, consequently, the shell's primary prompt string will change:

	$ PS1='hello: '
	hello: echo prompt is now $PS1 
	prompt is now hello: 

Notice that in the line which actually changes the value of the variable, the variable name does not have a dollar symbol in front of it. The dollar notation is only used when you are extracting a value from a variable, not when a value is being assigned. Also notice that, in the assignment, there are no spaces on either side of the equals (=) sign. If you put spaces into these positions the shell will not recognize the line as a variable assignment and will probably generate an error message.

In addition to the standard set of shell variables you are free to create your own variables and to assign values to them. The shell does not complain if you try to access the value of a variable which has not yet been assigned. The shell just returns an empty string as the value:

	$ echo $sn flowers are pretty
	flowers are pretty
	$ sn=sun
	$ echo $sn flowers are pretty
	sun flowers are pretty

If you wanted the previous output to be sunflowers are pretty then you would need to eliminate the space between the value of the variable sn and the word flowers. As you can see in the following example, the obvious solution to this problem doesn't work:

	$ echo $snflowers are pretty 
	are pretty

Just removing the space on the command line has caused the shell to treat the new word snflowers as a variable name, and one which currently has no value. The standard way round this problem is to enclose the required variable name in a pair of curly braces, thus separating the name from the following word, but without having a space between them:

	$ echo ${sn}flowers are pretty 
	sunflowers are pretty 

In general, it is acceptable to change the value of an existing variable just by re-assigning it a new value:

	$ sn='all kinds of '
	$ echo ${sn}flowers are pretty
	all kinds of flowers are pretty

In this example, the quotes around the string being assigned to the variable sn are needed because the string is to contain spaces. Normally, the shell will split a line of input into separate words, using the spaces as the split points. In this example it is required that the string of characters, including its spaces, all be assigned to the shell variable.

Sometimes you will want to declare a variable, set it to a particular value and then want to prevent that value from being changed. This can be done by setting the variable up as readonly, once its initial value has been assigned:

	$ readonly sn
	$ sn=sun
	bash: sn: read-only variable
	$ echo ${sn}flowers are pretty
	all kinds of flowers are pretty

As another example, there is also a standard shell variable, which you have already seen, that is set up as readonly - the UID variable. This obviously needs to be readonly as its value is set when the user logs in, from the user's UID in the password file, and is not a value which can just be changed at will.

Wheneyer you create a variable it is just local to the current shell. This means that the values of your local shell variables cannot be seen by other commands or scripts run from your shell. Quite often, it would be useful for these values to be made available to the commands that the shell executes. This can be done with the command export. In the previous example, the variable sa is a local variable, which can be made available to subsequent commands as follows:

	$ export sn

The export operation on a variable can also be performed at the same time as it is assigned a value, as a one line command:

	$ export sn=sun

NEXT UP previous
Next: Shell Script Parameters