Skip to main content

Example

Variables

Generally quote your variables unless they contain wildcards to expand or command fragments.

String quotes

Shell execution

See Command substitution

Conditional execution

Functions

See: Functions

Conditionals

See: Conditionals

Strict mode

See: Unofficial bash strict mode

Brace expansion

Lets take echo {A,B}.js as an example:
See: Brace expansion

Parameter expansions


Basics

See: Parameter expansion

Prefix name expansion

Indirection

Substitution

Comments

Substrings

Length

Manipulation

Default values

Omitting the : removes the (non)nullity checks, e.g. ${foo-val} expands to val if unset otherwise $foo.

Loops


Basic for loop

C-like for loop

Ranges

With step size

Reading lines

Forever

Functions


Defining functions

Returning values

Raising errors

Arguments

Note: $@ and $* must be quoted in order to perform as described. Otherwise, they do exactly the same thing (arguments as separate strings).
See Special parameters.

Conditionals

Conditions

Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.

More conditions

File conditions

Example

Arrays


Defining arrays

Working with arrays

Operations

Iteration

Dictionaries


Defining

Declares sound as a Dictionary object (aka associative array).

Working with dictionaries

Iteration

Iterate over values

Iterate over keys

Debugging/Options

Options

Glob options

Set GLOBIGNORE as a colon-separated list of patterns to be removed from glob matches.

History

Commands

Expansions

Operations

!! and !$ can be replaced with any valid expansion.

Slices

!! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc.

Miscellaneous


Numeric calculations

Subshells

Redirection

Inspecting commands

Trap errors

or

Case/switch

Source relative

printf

Example

Directory of script

Getting options

Heredoc

Reading input

The -r option disables a peculiar legacy behavior with backslashes.

Special variables

See Special parameters.

Go to previous directory

Check for command’s result

Grep check