Autosave: 2023-01-31 13:55:42

This commit is contained in:
thomasabishop 2023-01-31 13:55:42 +00:00
parent d828c70667
commit bd3bd0f284
4 changed files with 76 additions and 21 deletions

View file

@ -0,0 +1,74 @@
---
categories:
- Programming Languages
tags:
- shell
---
# Expansions and substitutions
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.
Below are the main forms of expansion and substitution:
| Representation | Name |
| -------------- | -------------------- |
| `~` . | Tilde expansion |
| `{...}` | Brace expansion |
| `${...}` | Parameter expansion |
| `$(...)` | Command substitution |
| `$((...))` | Arithmetic expansion |
## Brace expansion
Brace expansion is for changing a smaller part of a greater whole programmatically. This is best understood by looking at examples:
```
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
```
```
echo {1..5}
1 2 3 4 5
echo {5..1}
5 4 3 2 1
echo {a...c}
a b c
```
We can also set sequences. If we wanted to count to twenty in intervals of two:
```
echo {1..20..2}
1 3 5 7 9 11 13 15 17 19
```
> Note that we type _two_ dots **not** an elipsis
## Example use case
We might use brace expansion to generate sequential file names, eg.
```
touch file_{01..12}{a..d}
ls
file_01a
file_01b
file_01c
file_01d
file_02a
file_02b
...
file_12d
```

View file

@ -22,24 +22,6 @@ for ele in $A_STR_LIST; do
done
```
## Brace expansion for listed variables
With a sequence of variables that follow a pattern, for example the natural numbers (1, 2, 3, 4, ...) we can represent them in a condensed format using something called **brace expansion**. For instance to represent the natural numbers from 1 through 10:
```bash
{1..10}
```
Here the **two dots** stand for the intervening values.
We can iterate through brace expanded variables just the same:
```bash
for num in {1..4}; do
echo $num
done
```
## Arrays
We define an array as follows:

View file

@ -11,10 +11,9 @@ You'll see the following a lot when reading shell scripts:
```bash
[some_command] > /dev/null 2>&1
```
This is a redirection statement. It is redirecting data to the `null` device on Unix systems. Basically to a black hole or shredder where you can't access it because you don't want it to be output to stout.
This is a redirection statement. It is redirecting data to the `null` device on Unix systems. Basically to a black hole or shredder where you can't access it because you don't want it to be output to `stdout`.
The `2>&1` argument is the content: any errors that the program may generate and try to show in stout. Notice we are using the [file descriptors](/Programming_Languages/Shell/File_descriptors_and_redirection.md) `1` and `2`.

View file

@ -15,7 +15,7 @@ The symbol `>` is called the **redirection operator** because it redirects `stdo
ls | grep d* > result.txt
```
### Combining redirection with file descriptors
### Combining redirection with file escriptors
It is common practice to combine redirection with the [file descriptors](/Programming_Languages/Shell/File_descriptors.md) to redirect the output of `stdout` and `stderr`. A common case is to [redirect error output to `/dev/null`](/Programming_Languages/Shell/Redirect_to_dev_null.md).