Variables & Environment

Variable scoping, export, environment inspection, $PATH, $IFS, and Bash special variables.

Overview

Overview Bash variables and the environment are fundamental to shell scripting and interactive use. Getting them wrong causes subtle, hard-to-debug bugs.

Variable assignment and types: In bash, no type declaration is needed. var=value (no spaces around =). To use, prefix with $: echo $var or echo "${var}". Curly braces are required for ${var}suffix to disambiguate from $varsuffix.

Local vs global scope: Variables set in a script are global within that script. Variables set inside a function are global within the script unless declared with local. local var=value restricts the variable to the function scope and its children.

export: Makes a variable available to child processes (subshells, commands run from the script). Without export, child processes cannot see the variable. export VAR=value sets and exports. env or printenv shows exported variables. set shows all variables including unexported shell variables (much more output).

$PATH: The colon-separated list of directories searched for commands. Order matters — the first match is used. Add to PATH safely: export PATH="$HOME/.local/bin:$PATH" (prepend — takes priority) or PATH="$PATH:/opt/myapp/bin" (append). Never overwrite PATH entirely.

$IFS (Internal Field Separator): Controls word splitting. Default is space/tab/newline. Changing IFS affects for loop iteration and read. Set IFS=$' ' to iterate over lines with spaces without splitting. Always restore: old_IFS="$IFS"; IFS=$' '; ...; IFS="$old_IFS".

Special variables: $? (exit code of last command), $$ (current shell PID), $! (PID of last background job), $0 (script name), $1-$9 (positional parameters), $@ (all args, preserves quoting), $* (all args as one string), $# (argument count), $RANDOM (0-32767), $LINENO (current line number).

Key concepts

export keyword
export VAR=value makes VAR visible to child processes. Without export, child processes (commands, subshells) cannot see the variable. env shows all exported variables.
$PATH order
Shell searches directories left-to-right, uses first match. Prepend to take priority: PATH="$HOME/bin:$PATH". Never overwrite PATH entirely — you lose all system commands.
$IFS word splitting
IFS (Internal Field Separator) controls how bash splits words. Default: space/tab/newline. Set IFS=$'\n' to iterate over newline-separated data without splitting on spaces.
$? and $@ vs $*
$? is last exit code. "$@" expands to separate quoted args (preserves spacing). "$*" expands to one string with args joined by IFS. Always use "$@" in scripts.

When to use it

  • Writing robust shell scripts that pass data between functions
  • Debugging PATH and command-not-found issues
  • Setting up environments for deployment scripts
  • Understanding why a script works interactively but fails in cron

Interview questions

  • What is the difference between $@ and $* in bash? When does it matter?
  • How does the export keyword work, and what happens to variables without it in subshells?

Sign up free to read the full topic

More Linux topics

Start free on Cariara · Coding problems · System design studio