Skip to content

Commit

Permalink
Add multi range brace expansion to bash.md (#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iftakharpy authored and rstacruz committed Dec 13, 2023
1 parent 1dbdb9c commit de4bb16
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ keywords:
- Command substitution
---

Getting started
---------------
## Getting started

{: .-three-column}

### Introduction

{: .-intro}

This is a quick reference to getting started with Bash scripting.
Expand All @@ -44,6 +45,7 @@ echo $name # see below
echo "$name"
echo "${name}!"
```

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

```bash
Expand Down Expand Up @@ -78,6 +80,7 @@ git commit || echo "Commit failed"
```

### Functions

{: id='functions-example'}

```bash
Expand All @@ -91,6 +94,7 @@ echo "You are $(get_name)"
See: [Functions](#functions)

### Conditionals

{: id='conditionals-example'}

```bash
Expand Down Expand Up @@ -118,17 +122,17 @@ See: [Unofficial bash strict mode](http://redsymbol.net/articles/unofficial-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>&lcub;{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
Expand Down Expand Up @@ -247,8 +251,8 @@ echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)

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
Expand Down Expand Up @@ -299,8 +303,8 @@ while true; do
done
```

Functions
---------
## Functions

{: .-three-column}

### Defining functions
Expand Down Expand Up @@ -353,29 +357,29 @@ fi

### 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 |
Expand Down Expand Up @@ -405,7 +409,7 @@ Note that `[[` is actually a command/program that returns either `0` (true) or `
### File conditions

| Condition | Description |
| --- | --- |
| ----------------------- | ----------------------- |
| `[[ -e FILE ]]` | Exists |
| `[[ -r FILE ]]` | Readable |
| `[[ -h FILE ]]` | Symlink |
Expand Down Expand Up @@ -461,8 +465,7 @@ if [[ -e "file.txt" ]]; then
fi
```
Arrays
------
## Arrays
### Defining arrays
Expand Down Expand Up @@ -509,8 +512,8 @@ for i in "${arrayName[@]}"; do
done
```
Dictionaries
------------
## Dictionaries
{: .-three-column}
### Defining
Expand Down Expand Up @@ -556,8 +559,7 @@ for key in "${!sounds[@]}"; do
done
```
Options
-------
## Options
### Options
Expand All @@ -581,8 +583,7 @@ shopt -s globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b
Set `GLOBIGNORE` as a colon-separated list of patterns to be removed from glob
matches.
History
-------
## History
### Commands
Expand Down Expand Up @@ -625,9 +626,7 @@ History
`!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
Miscellaneous
-------------
## Miscellaneous
### Numeric calculations
Expand All @@ -640,7 +639,7 @@ $(($RANDOM%200)) # Random number 0..199
```
```bash
declare -i count # Declare as type integer
declare -i count # Declare as type integer
count+=1 # Increment
```
Expand Down Expand Up @@ -732,18 +731,18 @@ printf '%i+%i=%i\n' 1 2 3 4 5 9

### 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

Expand Down Expand Up @@ -838,10 +837,11 @@ fi
```

## 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)_

0 comments on commit de4bb16

Please sign in to comment.