@@ -14,11 +14,12 @@ keywords:
1414 - Command substitution
1515---
1616
17- Getting started
18- ---------------
17+ ## Getting started
18+
1919{: .-three-column}
2020
2121### Introduction
22+
2223{: .-intro}
2324
2425This is a quick reference to getting started with Bash scripting.
@@ -44,6 +45,7 @@ echo $name # see below
4445echo " $name "
4546echo " ${name} !"
4647```
48+
4749Generally quote your variables unless they contain wildcards to expand or command fragments.
4850
4951``` bash
@@ -78,6 +80,7 @@ git commit || echo "Commit failed"
7880```
7981
8082### Functions
83+
8184{: id='functions-example'}
8285
8386``` bash
@@ -91,6 +94,7 @@ echo "You are $(get_name)"
9194See: [ Functions] ( #functions )
9295
9396### Conditionals
97+
9498{: id='conditionals-example'}
9599
96100``` bash
@@ -118,17 +122,17 @@ See: [Unofficial bash strict mode](http://redsymbol.net/articles/unofficial-bash
118122echo {A,B}.js
119123```
120124
121- | Expression | Description |
122- | ---------- | ------------------- |
123- | ` {A,B} ` | Same as ` A B ` |
124- | ` {A,B}.js ` | Same as ` A.js B.js ` |
125- | ` {1..5} ` | Same as ` 1 2 3 4 5 ` |
125+ | Expression | Description |
126+ | ---------------------- | --------------------- |
127+ | ` {A,B} ` | Same as ` A B ` |
128+ | ` {A,B}.js ` | Same as ` A.js B.js ` |
129+ | ` {1..5} ` | Same as ` 1 2 3 4 5 ` |
130+ | <code >&lcub ; {1..3},{7..9}}</code > | Same as ` 1 2 3 7 8 9 ` |
126131
127132See: [ Brace expansion] ( https://web.archive.org/web/20230207192110/https://wiki.bash-hackers.org/syntax/expansion/brace )
128133
134+ ## Parameter expansions
129135
130- Parameter expansions
131- --------------------
132136{: .-three-column}
133137
134138### Basics
@@ -247,8 +251,8 @@ echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)
247251
248252Omitting the ` : ` removes the (non)nullity checks, e.g. ` ${foo-val} ` expands to ` val ` if unset otherwise ` $foo ` .
249253
250- Loops
251- -----
254+ ## Loops
255+
252256{: .-three-column}
253257
254258### Basic for loop
@@ -299,8 +303,8 @@ while true; do
299303done
300304```
301305
302- Functions
303- ---------
306+ ## Functions
307+
304308{: .-three-column}
305309
306310### Defining functions
353357
354358### Arguments
355359
356- | Expression | Description |
357- | --- | --- |
358- | ` $# ` | Number of arguments |
359- | ` $* ` | All positional arguments (as a single word) |
360- | ` $@ ` | All positional arguments (as separate strings) |
361- | ` $1 ` | First argument |
362- | ` $_ ` | Last argument of the previous command |
360+ | Expression | Description |
361+ | ---------- | ---------------------------------------------- |
362+ | ` $# ` | Number of arguments |
363+ | ` $* ` | All positional arguments (as a single word) |
364+ | ` $@ ` | All positional arguments (as separate strings) |
365+ | ` $1 ` | First argument |
366+ | ` $_ ` | Last argument of the previous command |
363367
364368** Note** : ` $@ ` and ` $* ` must be quoted in order to perform as described.
365369Otherwise, they do exactly the same thing (arguments as separate strings).
366370
367371See [ Special parameters] ( https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables ) .
368372
369- Conditionals
370- ------------
373+ ## Conditionals
374+
371375{: .-three-column}
372376
373377### Conditions
374378
375379Note 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.
376380
377381| Condition | Description |
378- | --- | --- |
382+ | ------------------------ | --------------------- |
379383| ` [[ -z STRING ]] ` | Empty string |
380384| ` [[ -n STRING ]] ` | Not empty string |
381385| ` [[ STRING == STRING ]] ` | Equal |
@@ -405,7 +409,7 @@ Note that `[[` is actually a command/program that returns either `0` (true) or `
405409### File conditions
406410
407411| Condition | Description |
408- | --- | --- |
412+ | ----------------------- | ----------------------- |
409413| ` [[ -e FILE ]] ` | Exists |
410414| ` [[ -r FILE ]] ` | Readable |
411415| ` [[ -h FILE ]] ` | Symlink |
@@ -461,8 +465,7 @@ if [[ -e "file.txt" ]]; then
461465fi
462466` ` `
463467
464- Arrays
465- ------
468+ # # Arrays
466469
467470# ## Defining arrays
468471
@@ -509,8 +512,8 @@ for i in "${arrayName[@]}"; do
509512done
510513` ` `
511514
512- Dictionaries
513- ------------
515+ # # Dictionaries
516+
514517{: .-three-column}
515518
516519# ## Defining
@@ -556,8 +559,7 @@ for key in "${!sounds[@]}"; do
556559done
557560` ` `
558561
559- Options
560- -------
562+ # # Options
561563
562564# ## Options
563565
@@ -581,8 +583,7 @@ shopt -s globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b
581583Set ` GLOBIGNORE` as a colon-separated list of patterns to be removed from glob
582584matches.
583585
584- History
585- -------
586+ # # History
586587
587588# ## Commands
588589
@@ -625,9 +626,7 @@ History
625626
626627`!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
627628
628-
629- Miscellaneous
630- -------------
629+ ## Miscellaneous
631630
632631### Numeric calculations
633632
@@ -640,7 +639,7 @@ $(($RANDOM%200)) # Random number 0..199
640639```
641640
642641```bash
643- declare -i count # Declare as type integer
642+ declare -i count # Declare as type integer
644643count+=1 # Increment
645644```
646645
@@ -732,18 +731,18 @@ printf '%i+%i=%i\n' 1 2 3 4 5 9
732731
733732# ## Transform strings
734733
735- | Command option | Description |
736- | ------------------ | --------------------------------------------------- |
737- | ` -c` | Operations apply to characters not in the given set |
738- | ` -d` | Delete characters |
739- | ` -s` | Replaces repeated characters with single occurrence |
740- | ` -t` | Truncates |
741- | ` [:upper:]` | All upper case letters |
742- | ` [:lower:]` | All lower case letters |
743- | ` [:digit:]` | All digits |
744- | ` [:space:]` | All whitespace |
745- | ` [:alpha:]` | All letters |
746- | ` [:alnum:]` | All letters and digits |
734+ | Command option | Description |
735+ | -------------- | --------------------------------------------------- |
736+ | ` -c` | Operations apply to characters not in the given set |
737+ | ` -d` | Delete characters |
738+ | ` -s` | Replaces repeated characters with single occurrence |
739+ | ` -t` | Truncates |
740+ | ` [:upper:]` | All upper case letters |
741+ | ` [:lower:]` | All lower case letters |
742+ | ` [:digit:]` | All digits |
743+ | ` [:space:]` | All whitespace |
744+ | ` [:alpha:]` | All letters |
745+ | ` [:alnum:]` | All letters and digits |
747746
748747# ### Example
749748
838837` ` `
839838
840839# # Also see
840+
841841{: .-one-column}
842842
843- * [Bash-hackers wiki](https://web.archive.org/web/20230406205817/https://wiki.bash-hackers.org/) _(bash-hackers.org)_
844- * [Shell vars](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
845- * [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
846- * [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
847- * [ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_
843+ - [Bash-hackers wiki](https://web.archive.org/web/20230406205817/https://wiki.bash-hackers.org/) _(bash-hackers.org)_
844+ - [Shell vars](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
845+ - [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
846+ - [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
847+ - [ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_
0 commit comments