03. Variables and Expressions (Part 1)

Overview

Topics

  • More sophisticate ways to use variables
  • Different types of expressions
  • Evaluating expressions

Review of Basic Use of Variables

  • A user-defined variable is a variable that the user creates either in a script or on the command line.
    • User-defined variables use mostly lowercase letters
    • If a variable is created in a script, only the script can access that variable when it runs. If the user creates a variable on the command line, the variable only exists in the current log in session
  • A system variable is a variable that is pre-defined to store specific information about the system. An example is the HOME environment variable that stores a user’s home directory path.
    • System variables are in all uppercase
    • Both the system and the user can access a system variable any time that the system is running.
  • A shell variable is a variable that is pre-defined and updated by the shell as it runs. An example is the # variable that stores the number of arguments that the user types on the command line
    • Shell variable names are mostly made of punctuation marks
    • Both the shell and the user can access a shell variable when the shell runs
  • To set a user-defined variable, use the format: varName=value
    • with no space before or after the = sign, and the value needs to be in quotes if there are spaces or shell metacharacters in the value
    • To access any variable, use $varName or ${varName}

Null Variables

  • If a variable is not yet defined, then it is considered a null variable
  • A null variable stores nothing, so when you access it you get nothing as a result
    • For example, if var is not yet defined, echo $var will print nothing to screen
  • You can explicitly define a null variable by assigning a null string or nothing to it
    • var1=“” var1 gets assigned a null string (double quotes with nothing in between)
    • var2= var2 gets assigned nothing
  • A more sophisticate way of clearing out a variable is to use unset
    • unset var1 same as var1=

Initializing Variables

  • When you access a null variable, you get an empty string. For example, if var is null, then echo $var will print nothing
  • When you access a variable, you can choose a default value for a variable if it is null by using the symbol :- and { } with the variable name
  • Example:
    • var=hello
    • echo ${var:-aString} will print hello since var is not null
    • echo $var will print hello since var is not null
    • unset var
    • echo ${var:-aString} will print aString since var is null
    • echo $var will print nothing since var is null
  • The default value is not assigned to the variable, the shell temporarily gives var the aString value when you use :- with var
  • When accessing variable, you can set a default value for a variable if it is null by using the symbol := and { } with the variable name
  • Example:
    • var=hello
    • echo ${var:=aString} will print hello since var is not null
    • echo $var will print hello since var is not null
    • unset var
    • echo ${var:=aString} will print aString since var is null
    • echo $var will print aString since var is now set with the initializing value
    • When you use := , the default value is assigned to the variable if it is null, and the variable will have this default value as long as nothing else is assigned to it

Filenames and Commands in a Variable

  • A variable can contain a single filename, which can be used anywhere a single filename is needed as input
    • fname=fileA
    • cat $fname same as: cat fileA
  • A variable can contain a filename with wildcards, which can be expanded just like wildcards on the command line
    • files=‘*file*’
    • ls $files same as: ls *file*
  • A variable can contain a simple command, which can be run just like a command is run on a command line
    • cmd=‘ls lab*’
    • $cmd same as: ls lab*
    • Note that only simple commands can be stored in a variable and run by accessing the variable name
  • For more complex commands that contain pipes, multiple input arguments, etc., use eval
    • cmd=“egrep ‘e’ * ” search for lines containing e in all files in current directory
    • $cmd will not work, get nothing as output
    • eval $cmd works
    • list=‘ls –l | tail’
    • $list will not work
    • eval $list works

read into a Variable

  • read can read each user input (whitespace delimited) into a variable
  • read n1 n2 n3 read 3 numbers from the user into 3 variables
  • If the user gives more input than the number of variables provided in the script, the last variable will contain the rest of the input, as a string
    • read n1 n2 read 1st input into n1, read 2nd and 3rd input into n2 as a string
  • To read from a file, use input redirection when running the script
    • For example, if a script has these 2 lines of code:
    • read line1
    • read line2
    • And the script is run with: myscript < fileA
    • Then the first line of fileA will be read into line1, and the second line of fileA will be read into line2
    • [Those who are seasoned programmers can see a loop being put to use to read all the lines of fileA]

Exporting a Variable

  • When a shell creates another shell to run a process, it is said to fork a child process. The original shell is called a parent process
  • The variables created in the parent process do not automatically get transferred to a child process. This means the child process does not “see” the variables of the parent process.
  • To transfer, or export, variables to a child process, use export
  • Example:
    • name=cis18c
    • echo $name cis18c is printed to screen
    • ksh fork a child Korn shell
    • echo $name get nothing, name is a null variable
    • exit go back to parent shell
    • export name name will now be exported to any child of this shell process
    • ksh fork child process again
    • echo $name cis18c is printed to screen