Bash Notes for Professionals

Programming, Bash · notes

Voir tous les documents en systèmes d'exploitation et cloud

Bash

Notes for ProfessionalsBash

Notes for Professionals

100+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial Bash group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with Bash

.................................................................................................................

2

.................................................................................................................................................

Section 1.1: Hello World

Section 1.2: Hello World Using Variables

Section 1.3: Hello World with User Input

Section 1.4: Importance of Quoting in Strings

Section 1.5: Viewing information for Bash built-ins

Section 1.6: Hello World in "Debug" mode

Section 1.7: Handling Named Arguments

.....................................................................................................................

......................................................................................................................

............................................................................................................

....................................................................................................

6

..................................................................................................................

....................................................................................................................

Chapter 2: Script shebang

Section 2.1: Env shebang

Section 2.2: Direct shebang

Section 2.3: Other shebangs

........................................................................................................................................

8

..............................................................................................................................................

..........................................................................................................................................

........................................................................................................................................

Chapter 3: Navigating directories

......................................................................................................................

10

Section 3.1: Absolute vs relative directories

Section 3.2: Change to the last directory

Section 3.3: Change to the home directory

Section 3.4: Change to the Directory of the Script

..............................................................................................................

..................................................................................................................

..............................................................................................................

..................................................................................................

10

Chapter 4: Listing Files

............................................................................................................................................

12

Section 4.1: List Files in a Long Listing Format

Section 4.2: List the Ten Most Recently Modified Files

Section 4.3: List All Files Including Dotfiles

Section 4.4: List Files Without Using ls

Section 4.5: List Files

Section 4.6: List Files in a Tree-Like Format

Section 4.7: List Files Sorted by Size

.........................................................................................................

12

............................................................................................

13

................................................................................................................

.....................................................................................................................

...................................................................................................................................................

.............................................................................................................

..........................................................................................................................

Chapter 5: Using cat

..................................................................................................................................................

16

Section 5.1: Concatenate files

Section 5.2: Printing the Contents of a File

Section 5.3: Write to a file

Section 5.4: Show non printable characters

Section 5.5: Read from standard input

Section 5.6: Display line numbers with output

Section 5.7: Concatenate gzipped files

.....................................................................................................................................

...............................................................................................................

...........................................................................................................................................

.............................................................................................................

.....................................................................................................................

.........................................................................................................

18

.....................................................................................................................

18

Chapter 6: Grep

...........................................................................................................................................................

20

Section 6.1: How to search a file for a pattern

.........................................................................................................

20

Chapter 7: Aliasing

.....................................................................................................................................................

21

........................................................................................................................................

........................................................................................................................................

.....................................................................................................................................

Section 7.1: Bypass an alias

Section 7.2: Create an Alias

Section 7.3: Remove an alias

Section 7.4: The BASH_ALIASES is an internal bash assoc array

Section 7.5: Expand alias

Section 7.6: List all Aliases

............................................................................................................................................

..........................................................................................................................................

..........................................................................

22

Chapter 8: Jobs and Processes

...........................................................................................................................

23

Section 8.1: Job handling

Section 8.2: Check which process running on specific port

............................................................................................................................................

23

....................................................................................

25

2

4

4

5

6

7

8

8

Publicité

8

10

10

10

13

13

14

14

14

16

16

17

17

18

21

21

21

22

22

Section 8.3: Disowning background job

Section 8.4: List Current Jobs

Section 8.5: Finding information about a running process

Section 8.6: List all processes

....................................................................................................................

.....................................................................................................................................

.....................................................................................................................................

26

.....................................................................................

25

Chapter 9: Redirection

.............................................................................................................................................

27

..................................................................................................................

...............................................................................................................................

..............................................................................................

.................................................................................................................................

Section 9.1: Redirecting standard output

Section 9.2: Append vs Truncate

Section 9.3: Redirecting both STDOUT and STDERR

Section 9.4: Using named pipes

Section 9.5: Redirection to network addresses

Section 9.6: Print error messages to stderr

Section 9.7: Redirecting multiple commands to the same file

Section 9.8: Redirecting STDIN

Section 9.9: Redirecting STDERR

Section 9.10: STDIN, STDOUT and STDERR explained

........................................................................................................

..............................................................................................................

...............................................................................

31

..................................................................................................................................

...............................................................................................................................

............................................................................................

Chapter 10: Control Structures

............................................................................................................................

34

Section 10.1: Conditional execution of command lists

Section 10.2: If statement

Section 10.3: Looping over an array

Section 10.4: Using For Loop to List Iterate Over Numbers

Section 10.5: continue and break

Section 10.6: Loop break

Section 10.7: While Loop

Section 10.8: For Loop with C-style syntax

Section 10.9: Until Loop

Section 10.10: Switch statement with case

Section 10.11: For Loop without a list-of-words parameter

.............................................................................................

............................................................................................................................................

..........................................................................................................................

....................................................................................

37

...............................................................................................................................

.............................................................................................................................................

.............................................................................................................................................

...............................................................................................................

...............................................................................................................................................

................................................................................................................

.....................................................................................

Chapter 11: true, false and : commands

..........................................................................................................

41

Section 11.1: Infinite Loop

Section 11.2: Function Return

Section 11.3: Code that will always/never be executed

.............................................................................................................................................

......................................................................................................................................

...........................................................................................

41

Chapter 12: Arrays

......................................................................................................................................................

42

.................................................................................................................................

....................................................................................................................

.................................................................................................................................

.......................................................................................................................................

..........................................................................................................................................

.................................................................................................................................

....................................................................................................................

.....................................................................................................

...................................................................................................................................

......................................................................................................................

Section 12.1: Array Assignments

Section 12.2: Accessing Array Elements

Section 12.3: Array Modification

Section 12.4: Array Iteration

Section 12.5: Array Length

Section 12.6: Associative Arrays

Section 12.7: Looping through an array

Section 12.8: Destroy, Delete, or Unset an Array

Section 12.9: Array from string

Section 12.10: List of initialized indexes

Section 12.11: Reading an entire file into an array

Section 12.12: Array insert function

Chapter 13: Associative arrays

....................................................................................................

48

............................................................................................................................

............................................................................................................................

Section 13.1: Examining assoc arrays

........................................................................................................................

50

Chapter 14: Functions

...............................................................................................................................................

52

Section 14.1: Functions with arguments

Section 14.2: Simple Function

Section 14.3: Handling flags and optional parameters

.....................................................................................................................

.....................................................................................................................................

...........................................................................................

53

Publicité

25

25

27

27

28

28

30

30

31

32

32

34

35

36

37

37

38

39

39

39

40

41

41

42

43

43

44

45

45

46

47

47

47

48

50

52

53

Section 14.4: Print the function definition

Section 14.5: A function that accepts named parameters

Section 14.6: Return value from a function

Section 14.7: The exit code of a function is the exit code of its last command

......................................................................................

...............................................................................................................

..................................................................................................................

.....................................................

55

Chapter 15: Bash Parameter Expansion

..........................................................................................................

57

Section 15.1: Modifying the case of alphabetic characters

Section 15.2: Length of parameter

Section 15.3: Replace pattern in string

Section 15.4: Substrings and subarrays

Section 15.5: Delete a pattern from the beginning of a string

Section 15.6: Parameter indirection

Section 15.7: Parameter expansion and filenames

Section 15.8: Default value substitution

Section 15.9: Delete a pattern from the end of a string

Section 15.10: Munging during expansion

Section 15.11: Error if variable is empty or unset

.....................................................................................

............................................................................................................................

......................................................................................................................

....................................................................................................................

................................................................................

60

...........................................................................................................................

..................................................................................................

.....................................................................................................................

..........................................................................................

.................................................................................................................

......................................................................................................

Chapter 16: Copying (cp)

.........................................................................................................................................

65

Section 16.1: Copy a single file

Section 16.2: Copy folders

....................................................................................................................................

65

...........................................................................................................................................

65

Chapter 17: Find

...........................................................................................................................................................

66

Section 17.1: Searching for a file by name or extension

Section 17.2: Executing commands against a found file

Section 17.3: Finding file by access / modification time

Section 17.4: Finding files according to size

Section 17.5: Filter the path

Section 17.6: Finding files by type

Section 17.7: Finding files by specific extension

..............................................................................................................

.........................................................................................................................................

..............................................................................................................................

........................................................................................................

..........................................................................................

66

.........................................................................................

66

..........................................................................................

Chapter 18: Using sort

..............................................................................................................................................

71

Section 18.1: Sort command output

Section 18.2: Make output unique

Section 18.3: Numeric sort

Section 18.4: Sort by keys

...........................................................................................................................

71

..............................................................................................................................

..........................................................................................................................................

...........................................................................................................................................

Chapter 19: Sourcing

.................................................................................................................................................

74

Section 19.1: Sourcing a file

Section 19.2: Sourcing a virtual environment

.........................................................................................................................................

74

...........................................................................................................

74

Chapter 20: Here documents and here strings

Section 20.1: Execute command with here document

Section 20.2: Indenting here documents

Section 20.3: Create a file

Section 20.4: Here strings

Section 20.5: Run several commands with sudo

Section 20.6: Limit Strings

..................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..........................................................................................................................................

Publicité

.....................................................................................................

...........................................................................................

76

.............................................................................................

Chapter 21: Quoting

...................................................................................................................................................

80

Section 21.1: Double quotes for variable and command substitution

Section 21.2: Dierence between double quote and single quote

Section 21.3: Newlines and control characters

Section 21.4: Quoting literal text

.........................................................................................................

.................................................................................................................................

....................................................................

80

.........................................................................

Chapter 22: Conditional Expressions

................................................................................................................

83

Section 22.1: File type tests

.........................................................................................................................................

83

54

54

55

57

57

58

59

61

61

62

62

63

64

67

68

69

70

70

71

71

72

76

76

77

77

78

78

80

81

81

83

85

85

85

86

86

88

88

89

90

91

92

92

93

93

93

94

96

97

98

98

Section 22.2: String comparison and matching

Section 22.3: Test on exit status of a command

Section 22.4: One liner test

Section 22.5: File comparison

Section 22.6: File access tests

Section 22.7: Numerical comparisons

Chapter 23: Scripting with Parameters

Section 23.1: Multiple Parameter Parsing

Section 23.2: Argument parsing using a for loop

Section 23.3: Wrapper script

Section 23.4: Accessing Parameters

Section 23.5: Split string into an array in Bash

Chapter 24: Bash history substitutions

.......................................................................................................

......................................................................................................

.........................................................................................................................................

....................................................................................................................................

....................................................................................................................................

.......................................................................................................................

...........................................................................................................

..................................................................................................................

....................................................................................................

89

......................................................................................................................................

.........................................................................................................................

.........................................................................................................

...........................................................................................................

....................................................................................................................................

...............................................................................................

.......................................................................................

......................................................................................

Section 24.1: Quick Reference

Section 24.2: Repeat previous command with sudo

Section 24.3: Search in the command history by pattern

Section 24.4: Switch to newly created directory with !#:N

Section 24.5: Using !$

Section 24.6: Repeat the previous command with a substitution

..................................................................................................................................................

..........................................................................

94

Chapter 25: Math

........................................................................................................................................................

95

Section 25.1: Math using dc

Section 25.2: Math using bash capabilities

Section 25.3: Math using bc

Section 25.4: Math using expr

........................................................................................................................................

95

...............................................................................................................

96

........................................................................................................................................

....................................................................................................................................

Chapter 26: Bash Arithmetic

.................................................................................................................................

98

Section 26.1: Simple arithmetic with (( ))

Section 26.2: Arithmetic command

Section 26.3: Simple arithmetic with expr

Publicité

...................................................................................................................

...........................................................................................................................

.................................................................................................................

99

Chapter 27: Scoping

................................................................................................................................................

100

Section 27.1: Dynamic scoping in action

.................................................................................................................

100

Chapter 28: Process substitution

......................................................................................................................

101

Section 28.1: Compare two files from the web

Section 28.2: Feed a while loop with the output of a command

Section 28.3: Concatenating files

Section 28.4: Stream a file through multiple programs at once

Section 28.5: With paste command

Section 28.6: To avoid usage of a sub-shell

Chapter 29: Programmable completion

Section 29.1: Simple completion using function

Section 29.2: Simple completion for options and filenames

.......................................................................................................

101

..........................................................................

101

............................................................................................................................

101

..........................................................................

101

........................................................................................................................

...........................................................................................................

.......................................................................................................

.....................................................................................................

................................................................................

Chapter 30: Customizing PS1

..............................................................................................................................

104

Section 30.1: Colorize and customize terminal prompt

Section 30.2: Show git branch name in terminal prompt

Section 30.3: Show time in terminal prompt

Section 30.4: Show a git branch using PROMPT_COMMAND

Section 30.5: Change PS1 prompt

Section 30.6: Show previous command return status and time

.........................................................................................

.....................................................................................

...........................................................................................................

..............................................................................

............................................................................................................................

..........................................................................

107

102

102

103

103

103

104

105

105

106

106

Chapter 31: Brace Expansion

...............................................................................................................................

109

Section 31.1: Modifying filename extension

.............................................................................................................

109

Section 31.2: Create directories to group files by month and year

Section 31.3: Create a backup of dotfiles

Section 31.4: Use increments

Section 31.5: Using brace expansion to create lists

Section 31.6: Make Multiple Directories with Sub-Directories

................................................................................................................

....................................................................................................................................

......................................................................

109

...............................................................................................

...............................................................................

Chapter 32: getopts : smart positional-parameter parsing

..............................................................

111

Section 32.1: pingnmap

.............................................................................................................................................

111

Chapter 33: Debugging

..........................................................................................................................................

113

Section 33.1: Checking the syntax of a script with "-n"

Section 33.2: Debugging using bashdb

Section 33.3: Debugging a bash script with "-x"

..................................................................................................................

....................................................................................................

..........................................................................................

113

Chapter 34: Pattern matching and regular expressions

.....................................................................

115

...........................................................

115

.........................................................................

..............................................................................

Section 34.1: Get captured groups from a regex match against a string

Section 34.2: Behaviour when a glob does not match anything

Section 34.3: Check if a string matches a regular expression

Section 34.4: Regex matching

Section 34.5: The * glob

Section 34.6: The ** glob

Section 34.7: The ? glob

Section 34.8: The [ ] glob

Section 34.9: Matching hidden files

Section 34.10: Case insensitive matching

Section 34.11: Extended globbing

..................................................................................................................................

............................................................................................................................................

...........................................................................................................................................

............................................................................................................................................

.........................................................................................................................................

.........................................................................................................................

................................................................................................................

.............................................................................................................................

Chapter 35: Change shell

......................................................................................................................................

121

Section 35.1: Find the current shell

Section 35.2: List available shells

Section 35.3: Change the shell

Chapter 36: Internal variables

..........................................................................................................................

121

.............................................................................................................................

........................