Add multi range brace expansion to bash.md (#2071)
This commit is contained in:
parent
1dbdb9c1aa
commit
de4bb16b2d
106
bash.md
106
bash.md
|
@ -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.
|
||||
|
@ -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
|
||||
|
@ -78,6 +80,7 @@ git commit || echo "Commit failed"
|
|||
```
|
||||
|
||||
### Functions
|
||||
|
||||
{: id='functions-example'}
|
||||
|
||||
```bash
|
||||
|
@ -91,6 +94,7 @@ echo "You are $(get_name)"
|
|||
See: [Functions](#functions)
|
||||
|
||||
### Conditionals
|
||||
|
||||
{: id='conditionals-example'}
|
||||
|
||||
```bash
|
||||
|
@ -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>{{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
|
||||
|
@ -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
|
||||
|
@ -299,8 +303,8 @@ while true; do
|
|||
done
|
||||
```
|
||||
|
||||
Functions
|
||||
---------
|
||||
## Functions
|
||||
|
||||
{: .-three-column}
|
||||
|
||||
### Defining functions
|
||||
|
@ -353,21 +357,21 @@ 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
|
||||
|
@ -375,7 +379,7 @@ Conditionals
|
|||
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 |
|
||||
|
@ -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 |
|
||||
|
@ -461,8 +465,7 @@ if [[ -e "file.txt" ]]; then
|
|||
fi
|
||||
```
|
||||
|
||||
Arrays
|
||||
------
|
||||
## Arrays
|
||||
|
||||
### Defining arrays
|
||||
|
||||
|
@ -509,8 +512,8 @@ for i in "${arrayName[@]}"; do
|
|||
done
|
||||
```
|
||||
|
||||
Dictionaries
|
||||
------------
|
||||
## Dictionaries
|
||||
|
||||
{: .-three-column}
|
||||
|
||||
### Defining
|
||||
|
@ -556,8 +559,7 @@ for key in "${!sounds[@]}"; do
|
|||
done
|
||||
```
|
||||
|
||||
Options
|
||||
-------
|
||||
## Options
|
||||
|
||||
### Options
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -625,9 +626,7 @@ History
|
|||
|
||||
`!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
|
||||
|
||||
|
||||
Miscellaneous
|
||||
-------------
|
||||
## Miscellaneous
|
||||
|
||||
### Numeric calculations
|
||||
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)_
|
||||
|
|
Loading…
Reference in New Issue