OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Basic Parameters

The simplest form of parameter expansion is reflected in the ordinary use of variables. For example:

$a

when expanded, becomes whatever the variable a contains. Simple parameters may also be surrounded by braces:

${a}

This has no effect on the expansion, but is required if the variable is adjacent to other text, which may confuse the shell. In this example, we attempt to create a filename by ap- pending the string “_file” to the contents of the variable a.


[me@linuxbox ~]$ a="foo"

[me@linuxbox ~]$ echo "$a_file"

[me@linuxbox ~]$ a="foo"

[me@linuxbox ~]$ echo "$a_file"


image


If we perform this sequence of commands, the result will be nothing, because the shell will try to expand a variable named a_file rather than a. This problem can be solved by adding braces around the “real” variable name:



[me@linuxbox ~]$ echo "${a}_file"

foo_file

[me@linuxbox ~]$ echo "${a}_file"

foo_file


We have also seen that positional parameters greater than 9 can be accessed by surround- ing the number in braces. For example, to access the eleventh positional parameter, we can do this:

${11}


Top OS Cloud Computing at OnWorks: