Wednesday, March 30, 2011

All About Variables

Format
VARIABLE_NAME = value
echo $VARIABLE_NAME  
Types
  • scalar variable -- holds 1 value at a time (aka name value pairs)
  • array variable -- holds 0 or more values
Variable Names
  • a-z, 0-9 or underscore
  • conventionally uppercase
  • start with letter
Setting Values

PET=fish
PET='fish bowl'
PET="fish bowl"

Setting Scalar Values

->PET= cat #A space will cause an error
ksh: cat:  not found

->PET=cat
->echo $PET
cat

->pet='cat fish'
->echo $pet
cat fish

->PET=ferret
->echo $pet   # $pet is not the same as $PET
cat fish  
->echo $PET
ferret

Setting Array Values

->PET[1]=FERRET
->PET[2]=parrot
->echo ${PET[1]}
FERRET
->echo ${PET[2]}
parrot
->echo ${PET[0]}# set since name is same as scalar variable
ferret

Set Variables to Read-only

->readonly PET # now PET can't be set to a value
->PET=dog
ksh: PET: is read only

Unset Variables (if not readonly)

->TREE=oak
->echo $TREE
oak
->unset TREE
->echo $TREE

Variable Types
  local
available within the current instance. not available to programs started by the shell
  environment
variable available to child processes Use export to create an environmental variable from a local variable                         export VARIABLE=value
  shell
special variables set by the shell

No comments:

Post a Comment