Difference between Local Variables and Environment Variables

Here we will learn about Local Variables and Environment Variables and the differences between them.
A variable is a label that equates to some value. The label always remain a constant. But the value can change over time, across systems or across accounts. The value may differ depending on who is running the shell script.

We can view a single variable and it’s value by enclosing the variable name in ${}.
This is called dereferencing or expanding the variable.
For example, executing the below command in terminal will show us the hostname of our system.

# echo ${HOSTNAME}


We can view all the variables and their values by using the below command.

# set -o posix && set && set +o posix
OPTIONS EXPLAINED

-o
Unset the default option

+o
Set the default option

posix
Conform more closely to POSIX standard
NOTE: && is the logical AND. It runs the second command only if the first command exited with status 0 (was successful).

NOTE: Bash by default, does not conform more closely to POSIX standard. In this default mode, set command prints out the functions along with variables. That is why we switch off the POSIX mode before displaying variables.
To know how Bash behaves in POSIX standard, goto http://gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html


Variables can be classified into two types.
1) Local Variables
2) Environment Variables

1) Local Variables

  • Also called Shell Variables.
  • Used for configuring the shell itself, storing data or command output.
  • Local to a single shell by default. They are not inherited by child shells.
  • Stored in /etc/bashrc and ~/.bashrc scripts.
  • Can be viewed using the below command
    # set -o posix && set > /tmp/set.txt && env > /tmp/env.txt && grep -vFf /tmp/env.txt /tmp/set.txt && rm -f /tmp/set.txt /tmp/env.txt && set +o posix
      OPTIONS EXPLAINED
    set -o Unset the default option +o Set the default option posix Conform more closely to POSIX standard
    grep -v Invert the sense of matching, to select non-matching lines. -F Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. -f Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.
    rm -f ignore nonexistent files, never prompt

    NOTE: && is the logical AND. It runs the second command only if the first command exited with status 0 (was successful).

    NOTE: Bash by default, does not conform more closely to POSIX standard. In this default mode, set command prints out the functions along with variables. That is why we switch off the POSIX mode before displaying variables.
    To know how Bash behaves in POSIX standard, goto http://gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html

  • Set with the below command syntax.
    syntax:    # VARIABLE=VALUE
    example: # HELLO=”Welcome to the system”
    NOTE: When setting variables at the command line, do not include spaces between the variable, the equals sign and the value.
  • examples:
    BASH           – Full file name used to invoke the instance of bash
    COLUMNS   – Width of terminal when printing selection lists
    HISTFILE     – Name of the file in which command history is saved
    PS1              – Appearance of the primary prompt string for bash

 

2) Environment Variables

  • Used to configure other commands or programs.
  • Inherited by child shells.
  • Stored in /etc/profile, /etc/profile.d/*, ~/.bash_profile, ~/.bash_login and ~/.profile scripts.
  • Can be viewed using any of the below commands.
    # env

    OR

    # printenv
  • Set with the below command syntax.
    syntax:    # export VARIABLE=VALUE
    example: # export HELLO=”Welcome to the system”
    NOTE: When setting variables at the command line, do not include spaces between the variable, the equals sign and the value.
  • examples:
    TERM            – Terminal type
    USER            – Username or ID string of the user
    USERNAME  – Optional name of the user
    HOME           – Home directory of the user

 

 

 

It is recommended to view the following sections also.
Difference between Login shell and Non Login shell
Bash startup scripts