Skip to main content

Declaring Variables

One strict rule: no space around the = sign:
Variable names must also start with a letter or underscore and can contain only letters, digits and underscores:

Types (or the Lack of Them)

Bash variables are string-like by default, and Bash has no dedicated boolean type. But it also supports indexed arrays, associative arrays in Bash 4 and newer, and attributes such as integer:
Arithmetic contexts interpret values as numbers, while normal expansions produce text. This is why comparison operators are split into separate categories for strings and integers — we cover those in the next part.

Special Variables

When we run a script, bash automatically populates these: $? is useful for debugging. We check it immediately after running a command to see its status. 0 means success; any non-zero value means failure.

[ ] vs [[ ]]

Both are used for conditionals but they are not the same thing. [ ] are not just brackets — [ is a command, usually provided as a shell builtin, that behaves like the test command. So [ "$a" -eq 1 ] performs the same test as test "$a" -eq 1. The closing ] is the final argument expected by the [ command. This is why the spaces inside are required:
[[ ]] is Bash-specific conditional syntax. It handles pattern matching and compound expressions and avoids word splitting and pathname expansion. When we are writing specifically for Bash, we generally prefer [[ ]].