The Darren Mulholland

Insert pretentious waffle here.

Bash Variable Cheatsheet

A no-frills guide to the Bash shell's variable syntax.



Shell variables are created and assigned using =.

foo=value
bar="value with spaces"

No spaces are allowed on either side of the =.

Values are accessed using a $, optionally with braces to disambiguate the variable name: $foo or ${foo}. Undefined variables evaluate to the empty string "".

Variables are local to the shell instance by default, but can be exported to become environment variables visible to subshells and subprocesses.

FOO=value
BAR=value
export FOO BAR

Environment variables can be created and exported on a single line.

export BAZ=value

Environment variables are traditionally given capslocked names.

Positional Parameters

Positional parameters are automatically defined variables that hold the command line arguments to scripts. They have the names 1, 2, 3, etc, so their values are denoted by $1, $2, $3, etc. Parameter 0 holds the name of the script itself.

Two special, confusingly-similar variables contain all of the positional parameters except 0* and @. The difference between them is only apparent when they are inside double quotes.

"$*" is a single string containing all of the positional parameters separated by the first character of the IFS environment variable (typically a space), i.e.

"$1 $2 $3 ... $N"

"$@" is a list of separate double-quoted strings separated by spaces, i.e.

"$1" "$2" "$3" ... "$N"

A final automatic variable # stores the number of arguments.

Function Variables

Functions have their own local set of positional parameters and automatic variables, just like scripts. The only point to note is that parameter 0 inside a function still refers to the script name, not the function name.

All other function variables are global by default. Defining or altering a variable within a function defines or alters that variable throughout the shell instance.

Local variables can be declared with the local keyword.

local foo bar=value baz="other value"

Local variables are visible only within their defining function and its children, i.e. functions called by the defining function.

String Operators

The following string operators can be used inside braces.

${name-default} Returns $name if the variable exists; otherwise returns default.
${name:-default} Returns $name if the variable exists and is not empty; otherwise returns default.
${name=default} Returns $name if the variable exists; otherwise sets name to default and returns the value.
${name:=default} Returns $name if the variable exists and is not empty; otherwise sets name to default and returns the value.
${name:offset:len} Returns the substring of name starting at offset and up to len characters. If len is omitted, the substring continues to the end of name.
${name#pattern} If pattern matches the beginning of $name, deletes the shortest part that matches and returns the rest.
${name##pattern} If pattern matches the beginning of $name, deletes the longest part that matches and returns the rest.
${name%pattern} If pattern matches the end of $name, deletes the shortest part that matches and returns the rest.
${name%%pattern} If pattern matches the end of $name, deletes the longest part that matches and returns the rest.
${name/pattern/str} Replaces the first match to pattern with str.
${name//pattern/str} Replaces all matches to pattern with str.
${#name} Returns the length of $name.

Command Substitution

Command substitution treats the output of a command as if it were the value of a variable.

$(command arg arg...)

The command is run in a subshell and its standard output, stripped of any trailing newlines, is returned as the value of the expression.

Command substitutions can be nested.