OSDN Git Service

Regular updates
[twpd/master.git] / bash.md
diff --git a/bash.md b/bash.md
index 1da454a..bb6114b 100644 (file)
--- a/bash.md
+++ b/bash.md
@@ -3,7 +3,7 @@ title: Bash scripting
 category: CLI
 layout: 2017/sheet
 tags: [Featured]
-updated: 2019-10-02
+updated: 2020-07-05
 keywords:
   - Variables
   - Functions
@@ -18,6 +18,14 @@ Getting started
 ---------------
 {: .-three-column}
 
+### Introduction
+{: .-intro}
+
+This is a quick reference to getting started with Bash scripting.
+
+- [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
+- [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
+
 ### Example
 
 ```bash
@@ -102,9 +110,11 @@ See: [Unofficial bash strict mode](http://redsymbol.net/articles/unofficial-bash
 echo {A,B}.js
 ```
 
-| `{A,B}` | Same as `A B` |
+| 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` |
+| `{1..5}`   | Same as `1 2 3 4 5` |
 
 See: [Brace expansion](http://wiki.bash-hackers.org/syntax/expansion/brace)
 
@@ -152,7 +162,7 @@ echo ${STR/foo/bar} # /path/to/bar.cpp
 ```bash
 STR="Hello world"
 echo ${STR:6:5}   # "world"
-echo ${STR:-5:5}  # "world"
+echo ${STR: -5:5}  # "world"
 ```
 
 ```bash
@@ -163,19 +173,19 @@ DIR=${SRC%$BASE}  #=> "/path/to/" (dirpath)
 
 ### Substitution
 
-| Code | Description |
-| --- | --- |
-| `${FOO%suffix}` | Remove suffix |
-| `${FOO#prefix}` | Remove prefix |
-| --- | --- |
-| `${FOO%%suffix}` | Remove long suffix |
-| `${FOO##prefix}` | Remove long prefix |
-| --- | --- |
-| `${FOO/from/to}` | Replace first match |
-| `${FOO//from/to}` | Replace all |
-| --- | --- |
-| `${FOO/%from/to}` | Replace suffix |
-| `${FOO/#from/to}` | Replace prefix |
+| Code              | Description         |
+| ----------------- | ------------------- |
+| `${FOO%suffix}`   | Remove suffix       |
+| `${FOO#prefix}`   | Remove prefix       |
+| ---               | ---                 |
+| `${FOO%%suffix}`  | Remove long suffix  |
+| `${FOO##prefix}`  | Remove long prefix  |
+| ---               | ---                 |
+| `${FOO/from/to}`  | Replace first match |
+| `${FOO//from/to}` | Replace all         |
+| ---               | ---                 |
+| `${FOO/%from/to}` | Replace suffix      |
+| `${FOO/#from/to}` | Replace prefix      |
 
 ### Comments
 
@@ -333,13 +343,16 @@ fi
 
 ### Arguments
 
-| Expression | Description                            |
-| ---        | ---                                    |
-| `$#`       | Number of arguments                    |
-| `$*`       | All arguments                          |
-| `$@`       | All arguments, starting from first     |
-| `$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](http://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables).
 
@@ -404,6 +417,8 @@ if [[ -z "$string" ]]; then
   echo "String is empty"
 elif [[ -n "$string" ]]; then
   echo "String is not empty"
+else
+  echo "This never happens"
 fi
 ```
 
@@ -561,15 +576,19 @@ History
 
 ### Commands
 
-| `history` | Show history |
+| Command               | Description                               |
+| --------------------- | ----------------------------------------- |
+| `history`             | Show history                              |
 | `shopt -s histverify` | Don't execute expanded result immediately |
 
 ### Expansions
 
-| `!$` | Expand last parameter of most recent command |
-| `!*` | Expand all parameters of most recent command |
-| `!-n` | Expand `n`th most recent command |
-| `!n` | Expand `n`th command in history |
+| Expression   | Description                                          |
+| ------------ | ---------------------------------------------------- |
+| `!$`         | Expand last parameter of most recent command         |
+| `!*`         | Expand all parameters of most recent command         |
+| `!-n`        | Expand `n`th most recent command                     |
+| `!n`         | Expand `n`th command in history                      |
 | `!<command>` | Expand most recent invocation of command `<command>` |
 
 ### Operations
@@ -607,7 +626,7 @@ $((a + 200))      # Add 200 to $a
 ```
 
 ```bash
-$((RANDOM%=200))  # Random number 0..200
+$(($RANDOM%200))  # Random number 0..199
 ```
 
 ### Subshells
@@ -630,6 +649,7 @@ python hello.py &>/dev/null    # stdout and stderr to (null)
 
 ```bash
 python hello.py < foo.txt      # feed foo.txt to stdin for python
+diff <(ls -r) <(ls)            # Compare two stdout without files
 ```
 
 ### Inspecting commands
@@ -689,6 +709,28 @@ printf "This is how you print a float: %f" 2
 #=> "This is how you print a float: 2.000000"
 ```
 
+### 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                              |
+
+#### Example
+
+```bash
+echo "Welcome To Devhints" | tr [:lower:] [:upper:]
+WELCOME TO DEVHINTS
+```
+
 ### Directory of script
 
 ```bash
@@ -735,10 +777,13 @@ read -n 1 ans    # Just one character
 
 ### Special variables
 
-| `$?` | Exit status of last task |
-| `$!` | PID of last background task |
-| `$$` | PID of shell |
-| `$0` | Filename of the shell script |
+| Expression | Description                            |
+| ---------- | -------------------------------------- |
+| `$?`       | Exit status of last task               |
+| `$!`       | PID of last background task            |
+| `$$`       | PID of shell                           |
+| `$0`       | Filename of the shell script           |
+| `$_`       | Last argument of the previous command  |
 
 See [Special parameters](http://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables).