Difference between revisions of "CLI Variables"

From FreekiWiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
== Introduction ==
 +
Environment variables are used for a variety of examples within the bash environment. They can be defined by the user or defined by bash. If they're created by bash then they're likely to be used by bash to define its behavior. Also, many bash scripts use environment variables to store information which will be used later in the script.
 +
 
== Commands ==
 
== Commands ==
 
  env  = Shows you the environment variables that are currently defined.
 
  env  = Shows you the environment variables that are currently defined.
Line 9: Line 12:
 
  BAR=Love
 
  BAR=Love
  
=== Example #2 - Derefrencing ===
+
=== Example #2 - De-refrencing ===
 
To get the value of the variable (i.e. dereference) you place a '$' before the variable name.
 
To get the value of the variable (i.e. dereference) you place a '$' before the variable name.
  
Line 16: Line 19:
 
  #echo $BAR
 
  #echo $BAR
 
  #Love
 
  #Love
 +
 +
=== Example #3 - Use in scripting ===
 +
Imagine you have the following conditional statement,
 +
 +
if [ ${FOO} -eq 1 ] ; then echo "TRUE" ; else echo "FALSE ; fi
 +
 +
  
 
== Quiz ==
 
== Quiz ==

Revision as of 12:57, 10 May 2008

Introduction

Environment variables are used for a variety of examples within the bash environment. They can be defined by the user or defined by bash. If they're created by bash then they're likely to be used by bash to define its behavior. Also, many bash scripts use environment variables to store information which will be used later in the script.

Commands

env  = Shows you the environment variables that are currently defined.
echo = Displays a line of text.

Examples

Example #1 - Assignment

To assign a value to an environment variable you simply type the variable name, '=', then the value. For example,

FOO=Rob
BAR=Love

Example #2 - De-refrencing

To get the value of the variable (i.e. dereference) you place a '$' before the variable name.

#echo $FOO
#Rob
#echo $BAR
#Love

Example #3 - Use in scripting

Imagine you have the following conditional statement,

if [ ${FOO} -eq 1 ] ; then echo "TRUE" ; else echo "FALSE ; fi


Quiz

  1. What is the value of the "USER" environment variable?
  2. What is the value of the "HOSTNAME" environment variable?
  3. What is the value of the "PWD" environment variable?
  4. What is the value of the "HOME" environment variable?
  5. How would you create an environment variable named GEEK that has the contents- "FREE GEEK is the best!"?
  6. Using the GEEK environment variable, how would you echo the statement, "I think that FREE GEEK is the best!"?
  7. Can you tab-complete environment variables?

Instructor Notes

There needs to be a little into paragraph and the examples could be flushed out more.