Most shells, including
, and , implement a flat variable namespace. According to , one of the lessons learned from is that a hierarchical namespace is better than a flat namespace. For this reason, amongst others, a hierarchical variable namespace was implemented by him in , with . (dot) as the separator for each level of the hierarchy. This expanded variable namespace enabled the implementation of an aggregate definition for a shell variable to include subvariables. Such shell variables are called . Not much appears to have been written about to date and, as usual, the man page is terse on the subject. This post will try to explain in detail and demonstrate how useful they can be in when dealing with structured data.As in
, , and , a variable in is defined by a pair.$ myvar=10 $ print $myvar 10
Now consider the following commands which show how to declare a
, define and interact with those .$ typeset -C myvar # declare 'myvar' as compound variable $ myvar.x=10 # set subvariable 'x' to 10 $ myvar.y=20 # set subvariable 'y' to 20 $ print $myvar # print definition of compound variable ( x=10 y=20 ) $ print ${myvar.x} # print value of 'x' subvariable 10 $ print ${myvar.y} # print value of 'y' subvariable 20 $ yourvar=( x=10 y=5 ) # declare and define 'yourvar' compound variable $ print $yourvar ( x=10 y=5 ) $ print $(( yourvar.x * yourvar.y )) # multiply 2 subvariables together. 50 $
A variable with a
in it’s name is called a . However to create a , a variable whose name consists of everything up to the period must already exist. Note that variable names that begin with are reserved for use by .Just as
initializes a simple variable, does the same for a . This is not assigning a value to ; it is simply declaring that is a and its value will be defined by all of the form .You can specify the type of specific
. For example, to create a with a named of type .You can have more than one level of
in a . However, you must be careful to first declare the otherwise you will get an error.You can use
with provided the types are compatable. When is applied to an arithmetic type, is added to the current value. When applied to a string variable, is appended to the value.You may be wondering why the value of a
is outputted in the form that it is. The reason is that value of a is intended to be in a form ready for reinput by as shown by the following example.You can copy
using the builtin.You can create a new
from part of a existing .can be exported but cannot.
One thing you need to be aware of is that
also uses the notation to denote what are called . Shell variables in can also behave as active objects rather than as simple storage units by having a one or more functions associated with a variable. These functions are called . A is defined like any other function, except that the name for a is formed by using the variable name, followed by a , followed by the discipline name. Any variable can have defined that are invoked when the variable is referenced or assigned a value. The default set of in is , , and . Other can be defined via a custom shared library.are currently a work in progress. Some of the examples that I have shown above may not work in future versions of . These examples were tested on .
Well, that is about all you need to get you started using
1. Copying via “eval” is obsolete – at least in ast-ksh.2008-11-04 you can simply do a a=b to copy the compound variable content from “b” to compound variable “a” (assuming both have been declared as compound variable)
2. Compound variables support the += operator to merge compound variables