Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash variables

Reference for Bash variables: assignment syntax, naming rules, quoting, local and exported scope, unset, readonly, default values and common mistakes.

A variable in Bash is a named piece of text (or an array of text) that the shell stores and substitutes when you write $name. There are no types in the usual sense: everything is a string, and arithmetic or comparisons decide how to interpret it. You create a variable simply by assigning to it; you read it with a dollar sign.

Understanding variables means understanding three things: the exact assignment syntax (no spaces around =), when the value is expanded and split (quote it), and who can see it (the current shell, functions, or child processes after export).

Terminal
name="Ada Lovelace"
echo "Hello, $name"

Try it

Bash Assignment, naming and quoting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Scope, export and defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
NAME=value                # assign (no spaces)
NAME+=more                # append
"$NAME"  "${NAME}"        # expand (quote it)
export NAME[=value]       # pass to child processes
local NAME[=value]        # function-local
readonly NAME[=value]     # constant
unset NAME                # remove
Name Description
Valid names Start with a letter or _, then letters, digits and _. Case matters: Name and name differ.
NAME=value Assignment. Spaces around = turn it into a command call. Value undergoes tilde, parameter, command and arithmetic expansion but no word splitting or globbing.
$NAME, ${NAME} Expansion. Use braces to separate the name from text and for modifiers like ${NAME:-default}.
export, local, readonly, declare Change scope, mutability or attributes (declare).

Errors and edge cases: name = value runs a command called name; assigning to a read-only variable is an error; an unset variable expands to an empty string unless set -u is on.

Usage notes

  • Always quote expansions: "$var". Unquoted values are split on whitespace and expanded as globs, a classic cause of bugs with file names containing spaces.
  • Variables are global by default. Inside functions use local for temporaries, or a helper can silently change the caller's data.
  • Export is one-way and one-time. Children get a copy at start-up; later changes in either side are not shared. A script cannot change its parent's variables unless it is sourced.
  • Prefix assignment. VAR=value command sets the variable only for that command's environment.
  • Uppercase is convention. Use uppercase for exported/environment variables and lowercase for your own so you do not clobber PATH, HOME or USER.
  • Unset vs. empty. ${v-x} substitutes only when unset; ${v:-x} also when empty. See Parameter expansion.
  • Integers and arrays. declare -i n makes assignments arithmetic; indexed and associative arrays are covered in their own pages.
  • Strict mode. set -u turns the use of an unset variable into an error, catching typos.