Shell and variables

How to use variables in the shell

view on github

Use variables

Table of contents

  1. Working with variables
  2. List of predefined environment variables
  3. User-defined variables
  4. Working with lists

Working with variables

  • The shell suports 2 types of variables : environment variables and shell variables.
  • Environment variables :
    • Are available system-wide and are inherited by all spawned child processes and shells.
    • Their values differ from user to user (an obvious example is $USER).
    • Environment variables for the current shell can be listed using env.
    • Environment variables can be modified and new variables added using env VARIABLE=value.
  • Shell variables :
    • Shell variables are variables that apply only to the current shell instance.
    • Each shell (zsh, bash, etc ...) has its own set of predefined internal shell variables.
    • Shell variables for the current shell can be listed using set.
    • Shell variables can be modified and new variables added using set VARIABLE=value.

Note : similar to shell commands, environment variables are accessibles from within shell scripts.


List of predefined environment variables

environment contains
$0 The name of the running executable
$1 - $9 The first 9 arguments that were passed to it
$# Total number of arguments that were passed
$@ All the arguments that were passed
$? The exit status of the most recently run process
$$ The PID (process id) of the current executable instance
$USER The username of the user running the process
$HOSTNAME The hostname of the machine the process is running on
$SECONDS The number of seconds since the process has started
$RANDOM Returns a different random number each time is it referred to
$LINENO Returns the current line number in the context of the shell or script

User-defined variables

  • User defined variables are most often used in shell scripts.
  • A variable is set using VARIABLE=value (no space on either side of the equals sign).
  • A variable is referred to using $VARIABLE.
  • Any variable included in a command will have the shell expansion replace it with its value before running the command.
  • The scope of a variable is the process it is created in.
  • As a result, referencing the same variable in different scripts requires the variable to be exportd as an environment variable.
  • Setting an environment variable in a script won't change its value outside of the script unless it is exported again.

Working with lists

  • Lists are actually lists of words that can either be :
    1. A series of strings, separated by spaces, ex : Stan Kyle Cartman.
    2. A range of numbers, separated by 2 dots, ex : {1..5} or {10..0} :
      • It evaluates as an increasing series of numbers if the right value is greater than the left value.
      • It evaluates as a decreasing series otherwise ...
      • It is possible to specify the increment/decrement value by adding two more dots (..) and the value to use.
      • For instance, {10..0..2} will evaluate to 10 8 6 4 2 0.
  • lists are not arrays : for instance range numbers can't be assigned to variables without being expanded into an array first.