Home‎ > ‎CIS 98‎ > ‎

Shell Variables

  1. Commandline Parsing
    The shell extracts "meaning" from the command line by identifying arguments and special shell tokens e.g. "<>|;$". It does this according to an internal grammar. When the shell cannot tokenize your command line, there is a syntax error. Which of the following command lines have syntax errors?
    1. echo one two three$TERM
    2. cal2003
    3. cal;echo February 2003
    4. who logins >
    5. who>logins
    6. <logins sort >who
    7. who |
    Does the shell always give you a syntax error when it can't parse the command line?
    What is needed to separate tokens from each other?
     
  2. User Variables
    All shell variables store character strings; they do not need to be declared before they are initialized.
    1. Variable identifiers
    2. Setting variables
      LPDIR=/usr/spool/lp
      FLOPPY=/dev/fd0
      PATH=/bin:/usr/bin:.
      LPDEST=laser5
      EXINIT="set number autowrite showmode"
      PRINTER=$LPDEST
      RESULTS=$(grep cis130 /etc/passwd)
      nada=
    3. Unsetting variables
      name=
      name=""
      unset name
      unset name1 name2 name3 ...
    4. Accessing variables
      cd $LPDIR
      tar tvf $FLOPPY
      echo $PATH
    5. ${name} notation
      MYVAR=word
      echo $MYVARs
      echo ${MYVAR}s
       
      ${var:-string}
      ${var:+string}
      ${var:=string}
    6. Exporting variables
      export name
      export name1 name2 name3 ...
      export -n name   # unexports the variable name
      export   # shows all exported variables
    7. Readonly variables
      readonly name
      readonly name1 name2 name3 ...
    8. Displaying variables
      echo
      set
      env
      export
  3. Variable Arithmetic
    The value of an arithmetic expression may be obtained using the '(( ))' construct.
    1. Syntax: $(( expression ))
      expression can be any arithmetic expression containing constants or variable names.
      Note: the $ preceding a variable is optional.
    2. Examples:
      1. echo $((2+3*5))   # echos the value: 17
      2. num=12; echo $((num + 1))   # echos the value: 13
      3. ((num++))   # simply increments the value of num displaying nothing to the screen.

  4. Shell Environment Variables
    HOME
    PATH
    LOGNAME
    MAILCHECK
    PS1
    PS2
    IFS
  5. The Shell Environment
    In general terms, the shell's environment is referring to all the data, and internal settings that the shell holds in its runtime data segment. It includes the file descriptors to all files that are currently opened by the shell, (at least stdin, stdout and stderr). The majority of the data space is occupied by all the variables the shell currently has defined.
     
    When a shell forks and creates a child process (often referred to as a subshell), the child process contains all the data of the parent shell. The only difference is that the child has different PID and PPID values. However, when the child shell execs another command, its data is replaced by the data from the new command. All of the original parent shell's data is lost except for those variables in the environment that were exported. Copies of those variables are passed along to the new command for its use.

Exercise 1: identifiers

Identify which of these are valid identifiers for user variables and give reasons why the others are invalid.
MYVAR
myvar12
12MYVAR
MYVAR 12
myvar-12
MYVAR_12
myvar#12
MY_VARIABLE_WHICH_IS_NUMBERED_TWELVE

Exercise 2: Arithmetic Expressions

Given the variables X and Y where X=5 and Y=7,
write the commandline that will:
  1. Display the sum of X and Y
  2. Assign the product of X and Y to the variable Z
  3. Double the value of the variable Y
  4. Decrement the value of X

Comments