2023-01-31 13:55:42 +00:00
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- shell
|
|
|
|
---
|
|
|
|
|
|
|
|
# Expansions and substitutions
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Bash is weird in that parentheses, braces and brackets are used not just as
|
|
|
|
markers for different code blocks but as the designators of commands in their
|
|
|
|
own right. The type of bracket you use effects how your input is interpreted.
|
2023-01-31 13:55:42 +00:00
|
|
|
|
|
|
|
Below are the main forms of expansion and substitution:
|
|
|
|
|
|
|
|
| Representation | Name |
|
|
|
|
| -------------- | -------------------- |
|
|
|
|
| `~` . | Tilde expansion |
|
2023-02-03 14:51:52 +00:00
|
|
|
| `{..}` | Brace expansion |
|
2023-01-31 13:55:42 +00:00
|
|
|
| `${...}` | Parameter expansion |
|
|
|
|
| `$(...)` | Command substitution |
|
|
|
|
| `$((...))` | Arithmetic expansion |
|
|
|
|
|
2023-02-03 14:51:52 +00:00
|
|
|
## Brace expansion: `{..}`
|
2023-01-31 13:55:42 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Brace expansion is for changing a smaller part of a greater whole
|
|
|
|
programmatically. This is best understood by looking at examples:
|
2023-01-31 13:55:42 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
echo c{a,o,u}t
|
|
|
|
cat cot cut
|
|
|
|
```
|
|
|
|
|
|
|
|
```
|
|
|
|
echo /tmp/{1..3}/file.txt
|
|
|
|
/tmp/1/file.txt /tmp/2/file.txt /tmp/3/file.txt
|
|
|
|
```
|
|
|
|
|
2023-02-14 15:37:40 +00:00
|
|
|
```bash
|
2023-01-31 13:55:42 +00:00
|
|
|
echo {1..5}
|
|
|
|
1 2 3 4 5
|
|
|
|
|
|
|
|
echo {5..1}
|
|
|
|
5 4 3 2 1
|
|
|
|
|
|
|
|
echo {a...c}
|
|
|
|
a b c
|
|
|
|
```
|
|
|
|
|
2023-02-03 14:51:52 +00:00
|
|
|
We can also set sequences. If we wanted to count to twenty in intervals of two
|
2023-01-31 13:55:42 +00:00
|
|
|
|
2023-02-14 15:37:40 +00:00
|
|
|
```bash
|
2023-01-31 13:55:42 +00:00
|
|
|
echo {1..20..2}
|
|
|
|
1 3 5 7 9 11 13 15 17 19
|
|
|
|
```
|
|
|
|
|
|
|
|
> Note that we type _two_ dots **not** an elipsis
|
|
|
|
|
2023-02-03 14:51:52 +00:00
|
|
|
### Example use case
|
2023-01-31 13:55:42 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We might use brace expansion to generate sequential file names using a
|
|
|
|
pre-defined naming scheme, eg.
|
2023-01-31 13:55:42 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
touch file_{01..12}{a..d}
|
|
|
|
ls
|
|
|
|
|
|
|
|
file_01a
|
|
|
|
file_01b
|
|
|
|
file_01c
|
|
|
|
file_01d
|
|
|
|
file_02a
|
|
|
|
file_02b
|
|
|
|
...
|
|
|
|
file_12d
|
2023-02-03 14:51:52 +00:00
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
The syntax here basically means: for each of the elements in the first list, run
|
|
|
|
the second list against them.
|
2023-02-03 14:51:52 +00:00
|
|
|
|
|
|
|
## Parameter expansion: `${...}`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We use most frequently for returning the value of stored
|
2024-06-16 18:45:04 +01:00
|
|
|
[variables](Variables_and_data_types_in_Bash.md).
|
2024-02-02 15:58:13 +00:00
|
|
|
Techically we do not have to use the braces, we can retrieve with just `$var`
|
|
|
|
however it's better to use them to minimise interpretation fuck-ups which happen
|
|
|
|
a lot.
|
2023-02-03 14:51:52 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
When the braces are used, this allows us to transform the values before they are
|
|
|
|
returned such as only returning from the 6th character: `${var:6}`.
|
2023-01-31 13:55:42 +00:00
|
|
|
|
2023-02-03 14:51:52 +00:00
|
|
|
## Command substition: `$(...)`
|
2023-01-31 13:55:42 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Command substitution (circle-brackets) allows us to put the output of one
|
|
|
|
command inside another. Bash runs the bracketed command in a
|
2024-02-17 11:57:44 +00:00
|
|
|
[sub-shell](Shell_sessions.md) and then returns it
|
2024-02-02 15:58:13 +00:00
|
|
|
to the main user shell.
|
2023-02-03 14:51:52 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
echo "The current directory is $(pwd)."
|
2023-01-31 13:55:42 +00:00
|
|
|
```
|
2023-02-03 14:51:52 +00:00
|
|
|
|
|
|
|
## Arithemtic expansion: `$((...))`
|
|
|
|
|
|
|
|
We use arithmetic expansion when we want to calculate numerical values
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
See
|
2024-02-17 11:57:44 +00:00
|
|
|
[Working with numbers in Bash](Working_with_numbers_in_Bash.md)
|
2024-02-02 15:58:13 +00:00
|
|
|
for more.
|