You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-86Lines changed: 93 additions & 86 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# Bash Scripting Guide
2
2
3
-
- Version: 1.0.0
4
-
- Author:
5
-
- Nathan Nellans
6
-
- Email: me@nathannellans.com
7
-
- Web:
8
-
-https://www.nathannellans.com
9
-
-https://github.com/nnellans/bash-guide
3
+
-Version: 1.0.1
4
+
-Author:
5
+
- Nathan Nellans
6
+
- Email: me@nathannellans.com
7
+
- Web:
8
+
-https://www.nathannellans.com
9
+
-https://github.com/nnellans/bash-guide
10
10
11
11
> [!IMPORTANT]
12
12
> This is an advanced guide and assumes you already know the basics of Bash. Think of this more like an advanced cheat sheet. I went through various sources, captured any notes that I felt were important, and organized them into the README file you see here.
@@ -19,31 +19,33 @@
19
19
20
20
---
21
21
22
-
# Table of Contents
23
-
-[Script File Basics](#script-file-basics)
24
-
-[Commands](#commands)
25
-
-[Variables](#variables)
26
-
-[Shell Variables](#shell-variables)
27
-
-[Environment Variables](#environment-variables)
28
-
-[Shell Functions](#shell-functions)
29
-
-[Local Variables](#local-variables)
30
-
-[Aliases](#aliases)
31
-
-[Viewing Variables, Functions, and Aliases](#viewing-variables-functions-and-aliases)
32
-
-[Standard Input, Output, and Error](#standard-input-output-and-error)
33
-
-[Pattern Matching](#pattern-matching)
34
-
-[Shell Arithmetic](#shell-arithmetic)
35
-
-[Shell Expansions](#shell-expansions)
36
-
-[If Statements](#if-statements)
37
-
-[test and [] commands](#conditional-1-the-test-and----commands)
You can define and use Local variables inside your functions.
279
-
- Local variables are visible only to the function where they are defined, and its children
280
-
- The names of Local variables will not conflict with other global variables defined elsewhere
281
-
- This helps you to create portable functions
285
+
-Local variables are visible only to the function where they are defined, and its children
286
+
-The names of Local variables will not conflict with other global variables defined elsewhere
287
+
- This helps you to create portable functions
282
288
283
289
Defining and using Local variables in a function:
284
290
@@ -308,7 +314,7 @@ function_name() {
308
314
> var_name="$(someCommand)"
309
315
>```
310
316
311
-
# Aliases
317
+
## Aliases
312
318
313
319
> [GSG](https://google.github.io/styleguide/shellguide.html): Aliases should be avoided in scripts. For almost every purpose, shell functions are preferred over aliases.
314
320
@@ -325,15 +331,15 @@ command2
325
331
alias_name
326
332
```
327
333
328
-
# Viewing Variables, Functions, and Aliases
334
+
##Viewing Variables, Functions, and Aliases
329
335
330
336
There are multiple commands to view the Shell variables, Environment variables, Functions, and Aliases that are defined in your environment. Below, you'll see a graphic I created that shows some of those commands, and what information each one will return.
331
337
332
338

333
339
334
340
---
335
341
336
-
# Standard Input, Output, and Error
342
+
##Standard Input, Output, and Error
337
343
338
344
| Stream | Purpose | Default | File Descriptor |
339
345
| --- | --- | --- | --- |
@@ -417,7 +423,7 @@ command &>> file.txt # append
417
423
418
424
---
419
425
420
-
# Pattern Matching
426
+
##Pattern Matching
421
427
422
428
Used in a few ways: parameter expansion, filename expansion, the `[[ … ]]` compound commands, the `case` statement, etc.
423
429
@@ -451,7 +457,7 @@ The following is not an exhaustive list:
451
457
452
458
---
453
459
454
-
# Shell Arithmetic
460
+
## Shell Arithmetic
455
461
456
462
Used in a few ways: arithmetic expansion, the `(( … ))` compound commands, the `let`&`expr` commands, `for` loops, etc.
- Word Splitting looks for spaces, tabs, and newline characters and treats them as delimiters between words
679
-
- Spaces, tabs, and newlines are the default value for the special Shell variable `$IFS` (which stands for Internal Field Separator)
680
-
- If you want Word Splitting to operate differently, then update the value of `$IFS`
681
-
- Word Splitting only occurs on the results of other expansions, namely Parameter Expansion, Command Substitution, and Arithmetic Expansion
682
-
- Word Splitting is NOT performed on anything inside double quotes
685
+
- Word Splitting looks for spaces, tabs, and newline characters and treats them as delimiters between words
686
+
- Spaces, tabs, and newlines are the default value for the special Shell variable `$IFS` (which stands for Internal Field Separator)
687
+
- If you want Word Splitting to operate differently, then update the value of `$IFS`
688
+
- Word Splitting only occurs on the results of other expansions, namely Parameter Expansion, Command Substitution, and Arithmetic Expansion
689
+
- Word Splitting is NOT performed on anything inside double quotes
683
690
684
691
### 7. Filename Expansion
685
692
@@ -706,7 +713,7 @@ ls /usr/bin/m*
706
713
707
714
---
708
715
709
-
# If Statements
716
+
## If Statements
710
717
711
718
```shell
712
719
if conditional;then
@@ -722,10 +729,10 @@ fi
722
729
> - Put `; then` at the end of the same line that contains `if` or `elif`
723
730
> - `fi` statements should be on their own line vertically aligned with the opening statement
724
731
725
-
- `conditional` can be any command, or set of commands, that will return an exit status
726
-
- In Bash, if a command is successful it has an exit status of `0`, and if it fails it will have a non-zero exit status
727
-
- `elif` and `else` are optional, only use them if you need to
728
-
- There are multiple ways to write your conditional expression. Three of those ways will be explored below
732
+
- `conditional` can be any command, or set of commands, that will return an exit status
733
+
- In Bash, if a command is successful it has an exit status of `0`, and if it fails it will have a non-zero exit status
734
+
- `elif` and `else` are optional, only use them if you need to
735
+
- There are multiple ways to write your conditional expression. Three of those ways will be explored below
729
736
730
737
### Conditional 1: the `test` and `[ … ]` commands
731
738
@@ -806,7 +813,7 @@ Newly added expressions:
806
813
807
814
---
808
815
809
-
# Case Statements
816
+
## Case Statements
810
817
811
818
- When `case` finds a match it will run those commands and then exit, with no subsequent matches being attempted. This behavior can be modified by changing `;;` to a different option (but also see the `GSG:` note below)
812
819
- As a fallback option, it is common to use `*` as the final pattern, as this will always match
> - Put `; do` at the end of the same line that contains `while`, `until`, or `for`
@@ -902,12 +909,12 @@ done
902
909
903
910
---
904
911
905
-
# Array Variables
912
+
## Array Variables
906
913
907
-
- Bash arrays are one-dimensional (think of a single-column spreadsheet)
908
-
- Arrays can be:
909
-
-"indexed" which means they use a zero-based numeric index
910
-
-"associative" which means they use a string-based index
914
+
- Bash arrays are one-dimensional (think of a single-column spreadsheet)
915
+
- Arrays can be:
916
+
- "indexed" which means they use a zero-based numeric index
917
+
- "associative" which means they use a string-based index
911
918
912
919
Naming standards: same as Shell variables
913
920
@@ -1008,14 +1015,14 @@ echo "${sorted_array[@]}"
1008
1015
1009
1016
---
1010
1017
1011
-
# Positional Parameters
1018
+
## Positional Parameters
1012
1019
1013
-
- Used in a few circumstances:
1014
-
- Arguments passed to the shell when its invoked
1015
-
- Arguments passed to a script
1016
-
- Arguments passed to a shell function
1017
-
- Can be reassigned using the `set`command
1018
-
- Positional Parameter `0` always expands to the name of the shell or shell script, which means the first actual passed argument will be at Positional Parameter `1`
1020
+
- Used in a few circumstances:
1021
+
- Arguments passed to the shell when its invoked
1022
+
- Arguments passed to a script
1023
+
- Arguments passed to a shell function
1024
+
- Can be reassigned using the `set`command
1025
+
- Positional Parameter `0` always expands to the name of the shell or shell script, which means the first actual passed argument will be at Positional Parameter `1`
0 commit comments