- Command substitution
---
-Getting started
----------------
+## Getting started
+
{: .-three-column}
### Introduction
+
{: .-intro}
This is a quick reference to getting started with Bash scripting.
echo "$name"
echo "${name}!"
```
+
Generally quote your variables unless they contain wildcards to expand or command fragments.
```bash
```
### Functions
+
{: id='functions-example'}
```bash
See: [Functions](#functions)
### Conditionals
+
{: id='conditionals-example'}
```bash
echo {A,B}.js
```
-| Expression | Description |
-| ---------- | ------------------- |
-| `{A,B}` | Same as `A B` |
-| `{A,B}.js` | Same as `A.js B.js` |
-| `{1..5}` | Same as `1 2 3 4 5` |
+| Expression | Description |
+| ---------------------- | --------------------- |
+| `{A,B}` | Same as `A B` |
+| `{A,B}.js` | Same as `A.js B.js` |
+| `{1..5}` | Same as `1 2 3 4 5` |
+| <code>{{1..3},{7..9}}</code> | Same as `1 2 3 7 8 9` |
See: [Brace expansion](https://web.archive.org/web/20230207192110/https://wiki.bash-hackers.org/syntax/expansion/brace)
+## Parameter expansions
-Parameter expansions
---------------------
{: .-three-column}
### Basics
Omitting the `:` removes the (non)nullity checks, e.g. `${foo-val}` expands to `val` if unset otherwise `$foo`.
-Loops
------
+## Loops
+
{: .-three-column}
### Basic for loop
done
```
-Functions
----------
+## Functions
+
{: .-three-column}
### Defining functions
### Arguments
-| Expression | Description |
-| --- | --- |
-| `$#` | Number of arguments |
-| `$*` | All positional arguments (as a single word) |
-| `$@` | All positional arguments (as separate strings) |
-| `$1` | First argument |
-| `$_` | Last argument of the previous command |
+| Expression | Description |
+| ---------- | ---------------------------------------------- |
+| `$#` | Number of arguments |
+| `$*` | All positional arguments (as a single word) |
+| `$@` | All positional arguments (as separate strings) |
+| `$1` | First argument |
+| `$_` | Last argument of the previous command |
**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](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables).
-Conditionals
-------------
+## Conditionals
+
{: .-three-column}
### 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.
| Condition | Description |
-| --- | --- |
+| ------------------------ | --------------------- |
| `[[ -z STRING ]]` | Empty string |
| `[[ -n STRING ]]` | Not empty string |
| `[[ STRING == STRING ]]` | Equal |
### File conditions
| Condition | Description |
-| --- | --- |
+| ----------------------- | ----------------------- |
| `[[ -e FILE ]]` | Exists |
| `[[ -r FILE ]]` | Readable |
| `[[ -h FILE ]]` | Symlink |
fi
```
-Arrays
-------
+## Arrays
### Defining arrays
done
```
-Dictionaries
-------------
+## Dictionaries
+
{: .-three-column}
### Defining
done
```
-Options
--------
+## Options
### Options
Set `GLOBIGNORE` as a colon-separated list of patterns to be removed from glob
matches.
-History
--------
+## History
### Commands
`!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
-
-Miscellaneous
--------------
+## Miscellaneous
### Numeric calculations
```
```bash
-declare -i count # Declare as type integer
+declare -i count # Declare as type integer
count+=1 # Increment
```
### Transform strings
-| Command option | Description |
-| ------------------ | --------------------------------------------------- |
-| `-c` | Operations apply to characters not in the given set |
-| `-d` | Delete characters |
-| `-s` | Replaces repeated characters with single occurrence |
-| `-t` | Truncates |
-| `[:upper:]` | All upper case letters |
-| `[:lower:]` | All lower case letters |
-| `[:digit:]` | All digits |
-| `[:space:]` | All whitespace |
-| `[:alpha:]` | All letters |
-| `[:alnum:]` | All letters and digits |
+| Command option | Description |
+| -------------- | --------------------------------------------------- |
+| `-c` | Operations apply to characters not in the given set |
+| `-d` | Delete characters |
+| `-s` | Replaces repeated characters with single occurrence |
+| `-t` | Truncates |
+| `[:upper:]` | All upper case letters |
+| `[:lower:]` | All lower case letters |
+| `[:digit:]` | All digits |
+| `[:space:]` | All whitespace |
+| `[:alpha:]` | All letters |
+| `[:alnum:]` | All letters and digits |
#### Example
```
## Also see
+
{: .-one-column}
-* [Bash-hackers wiki](https://web.archive.org/web/20230406205817/https://wiki.bash-hackers.org/) _(bash-hackers.org)_
-* [Shell vars](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
-* [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
-* [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
-* [ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_
+- [Bash-hackers wiki](https://web.archive.org/web/20230406205817/https://wiki.bash-hackers.org/) _(bash-hackers.org)_
+- [Shell vars](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
+- [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
+- [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
+- [ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_