This is an unofficial free book created for educational purposes and is not affiliated with the official Bash group(s) or company(s) or Stack Overflow. The information presented in this book is not guaranteed to be correct or accurate, use at your own risk.
Getting started with Bash
Hello World
Forget to use execute permission for the file ie. chmod +x hello-world.sh, the result is 1. Using sh ./hello-world.sh, not realizing that bash and sh are different shells with different properties ( although 3. since Bash is backwards compatible, the opposite is an error harmless).
Hello World Using Variables
Hello World with User Input
The read command here reads one line of data from standard input into the variable name.
Importance of Quoting in Strings
For more detailed information apart from beginner details, you can continue reading it here.
Viewing information for Bash built-ins
Hello World in "Debug" mode
The error indicated above is not sufficient to trace the script; However, using the following method will give you a better idea of where to look for the error in the script.
Handling Named Arguments
Script shebang
Env shebang
Direct shebang
Other shebangs
Navigating directories
Absolute vs relative directories
Change to the last directory
Change to the home directory
Change to the Directory of the Script
Listing Files
List Files in a Long Listing Format
List the Ten Most Recently Modified Files
List All Files Including Dotfiles
List Files Without Using `ls`
List Files
List Files in a Tree-Like Format
List Files Sorted by Size
Using cat
Concatenate files
Printing the Contents of a File
To display the contents of a file in a completely unambiguous byte-by-byte form, a hex dump is the standard solution. The default hex dump utility is od -chH, although the representation is a bit cumbersome; common replacements include xxd and hexdump.
Write to a file
Show non printable characters
Read from standard input
Display line numbers with output
Concatenate gzipped files
Note that greetings.txt.gz is a single file and is decompressed as the single greeting.txt file. Compare this to tar -czf hello.txt howdy.txt > greetings.tar.gz, which keeps the files separated within the tarball.
Grep
How to search a file for a pattern
Aliasing
Bypass an alias
Create an Alias
Remove an alias
The BASH_ALIASES is an internal bash assoc array
Expand alias
List all Aliases
Jobs and Processes
Job handling
Let's assume the command you want to execute and eventually kill is python test.py. This will create a file in the /tmp directory containing the pid of the python test.py process.
Check which process running on specific port
Even in this case if the file does not exist, the script assumes that you want to kill a non-running process. This last example can easily be improved to run the same command multiple times (eg at the pid file instead of overwriting it) and to handle cases where the process dies before it is killed.
Disowning background job
List Current Jobs
Finding information about a running process
For example, if you want to kill the nginx process, you can use the kill 5647 command.
List all processes
Redirection
Redirecting standard output
Append vs Truncate
Redirecting both STDOUT and STDERR
Using named pipes
Remember that the $pipedata variable is not available for use in the main terminal / main shell since the use of . It correctly prints the value of $pipedata variable in the main shell because of the export declaration of the variable.
Redirection to network addresses
Note that the contents of file3 are displayed first, followed by the ls -l data (LIFO configuration). Main terminal/main shell does not hang due to background shell invocation (&).
Print error messages to stderr
In this example, the error message will pollute the actual output of the script by mixing both errors and successful output into stdout. In the example above, the success message will be printed on stdout while the error message will be printed on stderr.
Redirecting multiple commands to the same file
Redirecting STDIN
Redirecting STDERR
STDIN, STDOUT and STDERR explained
In this case, if there is any STDERR, it will be redirected to /dev/null (a special file that ignores anything put into it), so you won't get any error output on the shell.
Control Structures
Conditional execution of command lists
Conditional execution is a hair faster than if..then, but its main advantage is that it allows functions and scripts to exit early or "short-circuit". The return statement will release whatever is local to the function and take over execution at the return address on the stack.
If statement
Returning to functions or exiting scripts as quickly as possible can significantly improve performance and reduce system load by avoiding unnecessary code execution. These are defined in the POSIX standard and are guaranteed to work in all POSIX-compatible shells, including Bash.
Looping over an array
Using For Loop to List Iterate Over Numbers
Loop break
While Loop
For Loop with C-style syntax
Until Loop
Switch statement with case
For Loop without a list-of-words parameter
Infinite Loop
Function Return
Code that will always/never be executed
Arrays
Array Assignments
Accessing Array Elements
Array Modification
Array Iteration
Array Length
Associative Arrays
Looping through an array
Destroy, Delete, or Unset an Array
Array from string
List of initialized indexes
Reading an entire file into an array
Array insert function
Associative arrays
Examining assoc arrays
Functions
Functions with arguments
Simple Function
Handling flags and optional parameters
If the message to print consists of the contents of a variable, the %s specifier must be used to print it, as in
Print the function definition
A function that accepts named parameters
Return value from a function
The exit code of a function is the exit code of its last command
For example, if host graucho is up, then connect to it with ssh: if is_alive graucho; then. Another example: repeatedly check until the graucho host is up, and then connect to it with ssh: while.
Bash Parameter Expansion
Modifying the case of alphabetic characters
Length of parameter
Note that this is the length in number of characters which is not necessarily the same as the number of bytes (as in UTF-8 where most characters are encoded in more than one byte), nor the number of glyphs/graphemes (some of which are combinations of characters), and it is also not necessarily the same as the display width.
Replace pattern in string
Substrings and subarrays
Delete a pattern from the beginning of a string
Parameter indirection
Parameter expansion and filenames
Default value substitution
Delete a pattern from the end of a string
Munging during expansion
Error if variable is empty or unset
Copying (cp)
Copy a single file
Copy folders
Find
Searching for a file by name or extension
Executing commands against a found file
In the example above, the -print0 and -0 flags specify that filenames will be separated using a null byte and allows the use of special characters, such as spaces, in filenames. You can pass all filenames in a single chmod call, using find. The problem with the above is that while read -r expects one entry per line, filenames can contain newlines (and also, read -r will lose any trailing whitespace).
This way the -exec receives the filenames in a form that is completely correct and portable; the bash -c receives them as a number of arguments, which can be found in $@, properly quoted etc.
Finding file by access / modification time
This is a GNU extension and may not work in other versions of find and xargs. The preferred way to do this is to skip the xargs command and let find call the subprocess itself:. Here {} is a placeholder indicating that you want to use the filename at that point. find will perform chmod on each file individually.
This is syntactically the most clumsy, but convenient when you want to run multiple commands on each file found.
Finding files according to size
Find files with n-block size, where +n means more than n-block, -n means less than n-block, and n (without any sign) means exactly n-block.
Filter the path
To find all files, all files except those in a folder called bin or log files:.
Finding files by type
Finding files by specific extension
Using sort
Sort command output
Make output unique
Numeric sort
Sort by keys
Sourcing
Sourcing a file
Sourcing a virtual environment
Here documents and here strings
Execute command with here document
Indenting here documents
Create a file
Here strings
Run several commands with sudo
Limit Strings
Quoting
Double quotes for variable and command substitution
Dierence between double quote and single quote
Newlines and control characters
Quoting literal text
Double quotes " delimit semi-literal strings with only the characters "\$ and ` retaining their special meaning.
Conditional Expressions
File type tests
String comparison and matching
Test on exit status of a command
One liner test
File comparison
File access tests
Numerical comparisons
Scripting with Parameters
Multiple Parameter Parsing
Argument parsing using a for loop
Wrapper script
For example, the actual egrep in the new GNU/Linux system is replaced by a wrapper script called egrep. So when you run egrep in such systems, you are essentially running grep -E with all arguments passed.
Accessing Parameters
Split string into an array in Bash
Bash history substitutions
Quick Reference
Allows flags before the s and alternate delimiters :s/foo/bar/ #substitutes bar for the first occurrence of foo :gs|foo|bar| #replaces bar for all foo.
Repeat previous command with sudo
Search in the command history by pattern
Switch to newly created directory with !#:N
Using !$
Repeat the previous command with a substitution
Math
Math using dc
Math using bash capabilities
Math using bc
Math using expr
Bash Arithmetic
Simple arithmetic with (( ))
Arithmetic command
Simple arithmetic with expr
Scoping
Dynamic scoping in action
Process substitution
Compare two files from the web
Feed a while loop with the output of a command
Concatenating files
Stream a file through multiple programs at once
The line count is sent to stderr (>&2) to avoid mixing with input to gzip.) Stdout from tee is simultaneously fed into gzip.
With paste command
To avoid usage of a sub-shell
Programmable completion
Simple completion using function
Simple completion for options and filenames
Customizing PS1
Colorize and customize terminal prompt
Make the changes to the file ~/.bashrc or /etc/bashrc or ~/.bash_profile or ~./profile (depending on the operating system) and save. For root, you may also need to edit the /etc/bash.bashrc or /root/.bashrc file. Run source code ~/.bashrc (distro-specific) after saving the file. Note: If you saved the changes to ~/.bashrc, don't forget to add source code ~/.bashrc to your .
Show git branch name in terminal prompt
Show time in terminal prompt
Show a git branch using PROMPT_COMMAND
Change PS1 prompt
D{format}format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation.
Show previous command return status and time
Brace Expansion
Modifying filename extension
Create directories to group files by month and year
Create a backup of dotfiles
Use increments
Using brace expansion to create lists
Make Multiple Directories with Sub-Directories
Debugging
Checking the syntax of a script with "-n"
Debugging using bashdb
Debugging a bash script with "-x"
Pattern matching and regular expressions
Get captured groups from a regex match against a string
Behaviour when a glob does not match anything
Check if a string matches a regular expression
Regex matching
The * glob
The ** glob
The ? glob
The [ ] glob
Matching hidden files
Case insensitive matching
Extended globbing
The following sub-patterns comprise valid extended globs: . pattern-list) – Matches zero or one occurrence of the given patterns. pattern-list) – Match zero or more occurrences of the given patterns +(pattern-list) – Match one or more occurrences of the given patterns. pattern-list) – Matches one of the given patterns. pattern-list) – Matches anything but one of the given patterns. The pattern list is a list of globs separated by. This extended glob itself can be used inside the negated extended glob. pattern list) to match macy. It matches anything that does not begin with zero or more occurrences of the letters r, s, and t, leaving only macy as a possible match.
Change shell
Find the current shell
List available shells
Change the shell
Internal variables
Bash internal variables at a glance
HISTSIZE
FUNCNAME
HOME
IFS
OLDPWD
PWD
To change the prefix, add your desired prefix to the end of the command line split file custom prefix. The trap is reset for subshells, so sleep will still act on the SIGINT signal sent by ^C (usually by exiting), but the parent process (ie the shell script) will not. In this case, the output of the ls command is used as input to the grep command.
A job can be a single command or a small script that needs to be run for each of the lines of the input. Ctrl + a move to the beginning of the line Ctrl + e move to the end of the line. Ctrl + k Kill the text from the current cursor position to the end of the line.
1 $2 $3 etc
RANDOM
BASHPID
BASH_ENV
BASH_VERSINFO
BASH_VERSION
EDITOR
HOSTNAME
HOSTTYPE
MACHTYPE
OSTYPE
PATH
PPID
SECONDS
SHELLOPTS
GROUPS
LINENO
SHLVL
This indicates that when a new shell is opened, an initial bash command runs and executes a task. The initial bash command executes a child process (another bash command) which in turn executes a final bash command to open the new shell. When the new shell opens, it runs as a child process of 2 other shell processes, hence the output of.
You can see that running the 'bash' command (or running a bash script) opens a new shell.
UID
Job Control
List background processes
Bring a background process to the foreground
Restart stopped background process
Run command in background
Stop a foreground process
Case statement
Simple case statement
Case statement with fall through
Fall through only if subsequent pattern(s) match
In the example below, the abc matches both first and third cases, but not the second case.
Read a file (data stream, variable) line-by-line (and/or field-by-field)?
Looping through a file line by line
Looping through the output of a command field by field
Read lines of a file into an array
Read lines of a string into an array
Looping through a string line by line
Looping through the output of a command line by line
Read a file field by field
Read a string field by field
Read fields of a file into an array
Read fields of a string into an array
Reads file (/etc/passwd) line by line and field by field
File execution sequence
Splitting Files
Split a file
File Transfer using scp
Downloading file using scp
Pipelines
Using |&
Show all processes paginated
Modify continuous output of a command
Managing PATH environment variable
Add a path to the PATH environment variable
Remove a path from the PATH environment variable
To make it permanent, you'll need to add it to the end of your bash configuration file. You will need to add these codes to your Bash configuration file (~/.bashrc or whatever).
Word splitting
What, when and Why?
Bad eects of word splitting
Usefulness of word splitting
Splitting by separator changes
Splitting with IFS
IFS & word splitting
Avoiding date using printf
Get the current date
Set variable to current time
Using "trap" to react to signals and system events
Introduction: clean up temporary files
Catching SIGINT or Ctl+C
Accumulate a list of trap work to run at exit
Killing Child Processes on Exit
Chain of commands and operations
Counting a text pattern ocurrence
Type of Shells
Start an interactive shell
Detect type of shell
Introduction to dot files
Color script output (cross-platform)
Hello World
Typing variables
Jobs at specific times
Execute job once at specific time
Doing jobs at specified times repeatedly using systemd.timer
Handling the system prompt
Using the PROMPT_COMMAND envrionment variable
Using PS2
Using PS3
Using PS4
Using PS1
The cut command
Only one delimiter character
Repeated delimiters are interpreted as empty fields
No quoting
Extracting, not manipulating
Bash on Windows 10
Readme
Cut Command
Show the first column of a file
Global variables
Local variables
Mixing the two together
CGI Scripts
Request Method: GET
Request Method: POST /w JSON
Select keyword
Select keyword can be used for getting input argument in a menu format
When to use eval
Using Eval
Using Eval with Getopt
Networking With Bash
Networking commands
The above command (Network Statistics) gives information about the connection and its status to www.google.com. The above command (domain information combiner) queries DNS-related information, nslookup www.google.com. The command above queries DNS to find the IP address corresponding to the site name.
The above command will add the default network route of interface eth0 to the routing table.
Parallel
Parallelize repetitive tasks on list of files
Parallelize STDIN
Decoding URL
Simple example
Using printf to decode a string
Design Patterns
The Publish/Subscribe (Pub/Sub) Pattern
Pitfalls
Whitespace When Assigning Variables
Failed commands do not stop script execution
Missing The Last Line in a File
Ctrl + u Kills the text from the current cursor position to the beginning of the line Ctrl + w Kills the word after the current cursor position. Killing text will delete text but save it so the user can insert it again by dragging. Similar to cut and paste, except that the text is placed on a kill ring which allows more than one set of text to be saved to drag back to the command line.
Ctrl + r search the history backwards Ctrl + p previous command in history Ctrl + n next command in history Ctrl + g exit history search mode.