CLI Variables

From FreekiWiki
Jump to navigation Jump to search

Introduction

Environment variables are used for a variety of reasons 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 and you wanted it to print "TRUE" to the screen.

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

The -eq statement checks to see if ${FOO} is equal to 1. Create an environment variable FOO that evaluates to 1 before running the statement.

FOO=1

This would cause the statement to evaluate to true and TRUE would be printed to the screen. In English it would say, "If 1 equals 1 then echo TRUE."

Example #4 - Customize your command prompt

Bash defines the PS1 environment variable to determine how the command prompt should look (see 'man bash'). If you change the value of the PS1 environment variable you will immediately change how your command prompt looks.

diskless241@gimpy:~$ PS1="What would you like to do? "
What would you like to do?

This hasn't changed anything other than how the command prompt looks. You can still run all of the commands that you were able to run before. It isn't recommended to change the command prompt to "What would you like to do?". It's cute, but that's about it. The command prompt was giving you 3 useful pieces of information; username, hostname and working directory. "What would you like to do?" doesn't give you any useful information and it's rather long.

Example #5 - Escaping symbols

Imagine you want to define a variable COST that you want to evaluate to "$10.99" you need to escape the '$' otherwise bash will try to substitute $1 and then "0.99".

diskless241@gimpy:~$ COST="$10.99"
diskless241@gimpy:~$ echo $COST
0.99

What you need to do is tell bash that you don't want the '$'

TBD

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.