How-to: Environment variables in bash

You can use variables in bash as in any programming language. There are no data types so a variable can contain a number, or a string of characters. There is no need to declare a variable, just assign a value:

STR="Hello World"
echo "$STR"

The VALUE of a variable is retrieved by placing a '$' before the variable name.

When echoing a variable it is important to surround the variable" in quotes", otherwise echo will expand the variable and attempt to perform globbing with any files that match in the current directory.

When you reference a variable in bash it is important not to put spaces around the equals sign: STR= 2, STR = 2, and STR =2 all mean slightly different things, the extra spaces will not throw a syntax error.

Bash variables may contain the characters: A-Z a-z 0-9 _ they are typically all-caps but lower or mixed case is also supported.

Shell variables are Global within bash, while an environment variable is visible to every process on the OS.

Reading (quoting) a variable

When reading a variable it is often useful to use parameter expansion to clearly indicate the variable name.
For example if you have a variable $MYVAR containing a filename (as a string) and want to do a rename, you might try:

mv "$MYVAR" "$MYVAR_bak" # this won’t work because bash will expect a variable called MYVAR_bak

Instead, replace $MYVAR with ${MYVAR} which expands to the string value we want.

mv "$MYVAR" "${MYVAR}_bak"

Example - using the $? shell parameter to obtain an exit code from ls.

ls ~/file-that-doesnt-exist
if [ "$EXITCODE" = "0" ]; then
  echo All OK
  echo File Not found


When a program is invoked it is given an array of strings called the environment.

This is a list of name-value pairs, of the form NAME=value.

Bash provides several ways to manipulate the environment. On invocation, the shell scans its own environment with a set of Default Shell variables and creates a parameter for each name found, automatically marking it for export to child processes.

Executed commands inherit the environment.
The export and 'declare -x' commands allow parameters and functions to be added to and deleted from the environment.

If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old.

The environment inherited by any executed command consists of the shell’s initial environment, whose values can be modified in the shell, less any pairs removed by the unset and 'export -n' commands, plus any additions via the export and 'declare -x' commands.

The environment for any simple command or function can be augmented temporarily by prefixing it with parameter assignments, as described in section Shell Parameters. These assignment statements affect only the environment seen by that command.

If the '-k' option is set, then all parameter assignments are placed in the environment for a command, not just those that precede the command name.

When Bash invokes an external command, the variable '$_' is set to the full path name of the command and passed to that command in its environment.

Related Linux commands

Bash shell variables
Windows equivalent: SET Set environment variables, env: drive in PowerShell.

Copyright © 1999-2024
Some rights reserved