Bash Scripting
Ctrl+Shift+C
: copy
Ctrl+Shift+C
: paste
Bash Use Modes
Interactive mode → shell waits for user's commands Non-interactive mode → shell runs scripts
File & Directories Permissions
File:
r
: Read. Can see file contentw
: Write. Can modify file contentx
: Execute. Can execute file
Directory:
r
: Read. Can see dir contentsw
: CRD. Can create, rename and delete filesx
: Search. Can access and navigate inside the dir. Necessary to operate on files
File Descriptors
FD 0
"standard input" → Channel for standard input (default keyboard)
FD 1
"standard output" → Channel for the default output (default screen)
FD 2
"standard error" → Channel for error messages, info messages, prompts (default keyboard)
File descriptors chan be joined to create streams that lead to files, devices or other processes.
Bash gets commands by reading lines.
As soon as it's read enough lines to compose a complete command, bash begins running that command.
Usually, commands are just a single line long. An interactive bash session reads lines from you at the prompt.
Non-interactive bash processes read their commands from a file or stream.
Files with a shebang as their first line (and the executable permission) can be started by your system's kernel like any other program.
First Line Of Bash
#!/bin/env bash
shebang indicating which interpreter to use
Simple Command
Bash | |
---|---|
Pipelines (commands concatenation)
Bash | |
---|---|
Lists (sequence of commands)
Bash | |
---|---|
COMPOUND COMMANDs (multiple commands as one)
Bash | |
---|---|
Functions (blocks of easily reusable code)
function_name () {compound_command}
Bash does not accept func arguments, parentheses must be empty
Command names & Running programs
To run a command, bash uses the name of your command and performs a search for how to execute that command.
In order, bash will check whether it has a function or builtin by that name.
Failing that, it will try to run the name as a program.
If bash finds no way to run your command, it will output an error message.
The path to a program
When bash needs to run a program, it uses the command name to perform a search.
Bash searches the directories in your PATH environment variable, one by one, until it finds a directory that contains a program with the name of your command.
To run a program that is not installed in a PATH directory, use the path to that program as your command's name.
Command arguments & Quoting literals
To tell a command what to do, we pass it arguments. In bash, arguments are tokens, that are separated from each other by blank space.
To include blank space in an argument's value, you need to either quote the argument or escape the blank space within.
Failing that, bash will break your argument apart into multiple arguments at its blank space.
Quoting arguments also prevents other symbols in it from being accidentally interpreted as bash code.
Managing a command's input and output using redirection
By default, new commands inherit the shell's current file descriptors.
We can use redirections to change where a command's input comes from and where its output should go to.
File redirection allows us to stream file descriptors to files.
We can copy file descriptors to make them share a stream. There are also many other more advanced redirection operators.
Redirections
Pathname Expansion (filname pattern [glob] matching)
*
matches any kind of text (even no text).
?
matches any single character.
[characters]
matches any single character in the given set.
[[:classname:]]
specify class of characters to match.
{}
expand list of arguments (applies command to each one)
shopt -s extglob
enables extended globs (patterns)
+(pattern [| pattern ...])
matches when any of the patterns in the list appears, once or many times over. ("at least one of ...").
*(pattern [| pattern ...])
matches when any of the patterns in the list appears, once, not at all, or many times over. ("however many of ...").
?(pattern [| pattern ...])
matches when any of the patterns in the list appears, once, not at all, or many times over. ("however many of ...").
@(pattern [| pattern ...])
matches when any of the patterns in the list appears just once. ("one of ...").
!(pattern [| pattern ...])
matches only when none of the patterns in the list appear. ("none of ...").
Command Substitution
With Command Substitution, we effectively write a command within a command, and we ask bash to expand the inner command into its output and use that output as argument data for the main command.
Bash | |
---|---|
Shell Variables
Parameter Expansion Modifiers (in double-quotes)
${parameter#pattern}
removes the shortest string that matches the pattern if it's at the start of the value.
${parameter##pattern}
removes the longest string that matches the pattern if it's at the start of the value.
${parameter%pattern}
removes the shortest string that matches the pattern if it's at the end of the value.
${parameter%%pattern}
removes the longest string that matches the pattern if it's at the end of the value.
${parameter/pattern/replacement}
replaces the first string that matches the pattern with the replacement.
${parameter//pattern/replacement}
replaces each string that matches the pattern with the replacement.
${parameter/#pattern/replacement}
replaces the string that matches the pattern at the beginning of the value with the replacement.
${parameter/%pattern/replacement}
replaces the string that matches the pattern at the end of the value with the replacement.
${#parameter}
expands the length of the value (in bytes).
${parameter:start[:length]}
expands a part of the value, starting at start, length bytes long.
Counts from the end rather than the beginning by using a (space followed by a) negative value.
${parameter[^|^^|,|,,][pattern]}
expands the transformed value, either upper-casing or lower-casing the first or all characters that match the pattern.
Omit the pattern to match any character.
Decision Statements
If Statement
Only the final exit code after executing the entire list is relevant for the branch's evaluation.
Bash | |
---|---|
Test Command
[[ argument_1 <operator> argument_2 ]]
Arithmetic expansion and evaluation
(( expression ))
Comparison Operators
Bash | |
---|---|
Arithmetic Comparison Operators
Bash | |
---|---|
String Comparison Operators
Commands short circuit evaluation
Bash | |
---|---|