@@ -14,11 +14,12 @@ keywords:
14
14
- Command substitution
15
15
---
16
16
17
- Getting started
18
- ---------------
17
+ ## Getting started
18
+
19
19
{: .-three-column}
20
20
21
21
### Introduction
22
+
22
23
{: .-intro}
23
24
24
25
This is a quick reference to getting started with Bash scripting.
@@ -44,6 +45,7 @@ echo $name # see below
44
45
echo " $name "
45
46
echo " ${name} !"
46
47
```
48
+
47
49
Generally quote your variables unless they contain wildcards to expand or command fragments.
48
50
49
51
``` bash
@@ -78,6 +80,7 @@ git commit || echo "Commit failed"
78
80
```
79
81
80
82
### Functions
83
+
81
84
{: id='functions-example'}
82
85
83
86
``` bash
@@ -91,6 +94,7 @@ echo "You are $(get_name)"
91
94
See: [ Functions] ( #functions )
92
95
93
96
### Conditionals
97
+
94
98
{: id='conditionals-example'}
95
99
96
100
``` bash
@@ -118,17 +122,17 @@ See: [Unofficial bash strict mode](http://redsymbol.net/articles/unofficial-bash
118
122
echo {A,B}.js
119
123
```
120
124
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 ` |
126
131
127
132
See: [ Brace expansion] ( https://web.archive.org/web/20230207192110/https://wiki.bash-hackers.org/syntax/expansion/brace )
128
133
134
+ ## Parameter expansions
129
135
130
- Parameter expansions
131
- --------------------
132
136
{: .-three-column}
133
137
134
138
### Basics
@@ -247,8 +251,8 @@ echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)
247
251
248
252
Omitting the ` : ` removes the (non)nullity checks, e.g. ` ${foo-val} ` expands to ` val ` if unset otherwise ` $foo ` .
249
253
250
- Loops
251
- -----
254
+ ## Loops
255
+
252
256
{: .-three-column}
253
257
254
258
### Basic for loop
@@ -299,8 +303,8 @@ while true; do
299
303
done
300
304
```
301
305
302
- Functions
303
- ---------
306
+ ## Functions
307
+
304
308
{: .-three-column}
305
309
306
310
### Defining functions
353
357
354
358
### Arguments
355
359
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 |
363
367
364
368
** Note** : ` $@ ` and ` $* ` must be quoted in order to perform as described.
365
369
Otherwise, they do exactly the same thing (arguments as separate strings).
366
370
367
371
See [ Special parameters] ( https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables ) .
368
372
369
- Conditionals
370
- ------------
373
+ ## Conditionals
374
+
371
375
{: .-three-column}
372
376
373
377
### Conditions
374
378
375
379
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.
376
380
377
381
| Condition | Description |
378
- | --- | --- |
382
+ | ------------------------ | --------------------- |
379
383
| ` [[ -z STRING ]] ` | Empty string |
380
384
| ` [[ -n STRING ]] ` | Not empty string |
381
385
| ` [[ STRING == STRING ]] ` | Equal |
@@ -405,7 +409,7 @@ Note that `[[` is actually a command/program that returns either `0` (true) or `
405
409
### File conditions
406
410
407
411
| Condition | Description |
408
- | --- | --- |
412
+ | ----------------------- | ----------------------- |
409
413
| ` [[ -e FILE ]] ` | Exists |
410
414
| ` [[ -r FILE ]] ` | Readable |
411
415
| ` [[ -h FILE ]] ` | Symlink |
@@ -461,8 +465,7 @@ if [[ -e "file.txt" ]]; then
461
465
fi
462
466
` ` `
463
467
464
- Arrays
465
- ------
468
+ # # Arrays
466
469
467
470
# ## Defining arrays
468
471
@@ -509,8 +512,8 @@ for i in "${arrayName[@]}"; do
509
512
done
510
513
` ` `
511
514
512
- Dictionaries
513
- ------------
515
+ # # Dictionaries
516
+
514
517
{: .-three-column}
515
518
516
519
# ## Defining
@@ -556,8 +559,7 @@ for key in "${!sounds[@]}"; do
556
559
done
557
560
` ` `
558
561
559
- Options
560
- -------
562
+ # # Options
561
563
562
564
# ## Options
563
565
@@ -581,8 +583,7 @@ shopt -s globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b
581
583
Set ` GLOBIGNORE` as a colon-separated list of patterns to be removed from glob
582
584
matches.
583
585
584
- History
585
- -------
586
+ # # History
586
587
587
588
# ## Commands
588
589
@@ -625,9 +626,7 @@ History
625
626
626
627
`!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
627
628
628
-
629
- Miscellaneous
630
- -------------
629
+ ## Miscellaneous
631
630
632
631
### Numeric calculations
633
632
@@ -640,7 +639,7 @@ $(($RANDOM%200)) # Random number 0..199
640
639
```
641
640
642
641
```bash
643
- declare -i count # Declare as type integer
642
+ declare -i count # Declare as type integer
644
643
count+=1 # Increment
645
644
```
646
645
@@ -732,18 +731,18 @@ printf '%i+%i=%i\n' 1 2 3 4 5 9
732
731
733
732
# ## Transform strings
734
733
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 |
747
746
748
747
# ### Example
749
748
838
837
` ` `
839
838
840
839
# # Also see
840
+
841
841
{: .-one-column}
842
842
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