This crash course offers a comprehensive introduction to Bash shell scripting for beginners, covering its definition, advantages (most used, cross-platform), and limitations (lacks OOP, complex syntax). It's suitable for Windows (WSL), Linux, and macOS users with basic Linux knowledge. The course guides learners through basic commands like echo and cat, writing and executing their first Bash script, managing file permissions (chmod), and utilizing variables (including interactive input). It also explains positional arguments, output redirection (>, >>), input redirection (<, <<, <<<), and test operators with exit codes. Control structures such as if/elif/else and case statements, including parameter expansion, are detailed. Advanced topics include arrays, for loops, functions (emphasizing local variables and arguments), and powerful text manipulation tools like AWK for filtering and SED for substitution with backup creation.
This crash course introduces Bash shell scripting, targeting beginners, those wishing to expand their knowledge, or those looking for a refresher.
Before scripting, it's essential to understand fundamental terminal commands.
echo Hello).echo Hello is a positional argument.:w to write (save).:q to quit.:wq to write and quit.:q! to quit without saving changes.cat textfile.txt).echo or vim operations successfully wrote content to a file.
Scripts automate tasks by combining commands into a single executable file.
shelltest.sh) using vim.echo "Hello World!").ls (list files) and pwd (print working directory).#!/bin/bash, is called the "shebang."echo $SHELL can reveal the path to the current shell interpreter.chmod) [00:10:35]ls -l to view file permissions (the long format of ls).chmod u+x shelltest.sh../shelltest.sh (where . refers to the current directory).
Variables store data that can be reused throughout a script.
VARIABLE_NAME=value (no spaces around =).$VARIABLE_NAME.FIRST_NAME=Herbert then echo "Hello $FIRST_NAME" outputs "Hello Herbert".
read) [00:14:20]read command prompts the user for input and stores it in a variable.echo "What is your first name?" then read FIRST_NAME.Arguments passed to a script at specific positions.
$1 refers to the first argument, $2 to the second, and so on.$0 is reserved for the shell itself (the script name).
read commands.posargu.sh containing echo "Hello $1 $2" can be run as ./posargu.sh Herbert Lindemans to output "Hello Herbert Lindemans".Controlling where command output goes and where input comes from.
|) [00:16:54]| sends the standard output of one command as the standard input to another command.ls -l /usr/bin | grep bash will list files in /usr/bin and then filter for lines containing "bash".
>, >>) [00:18:18]> redirects command output to a file, overwriting its contents if it exists.>> appends command output to a file, adding to its existing contents.
<, <<, <<<) [00:20:37]< redirects input from a file to a command. Example: wc -w < hello.txt counts words in hello.txt without printing the filename.<< EOF (here-document) allows multi-line input to a command until "EOF" (or any chosen delimiter) is encountered.<<< "string" (here-string) provides a single string as input to a command. Example: wc -w <<< "Hello there wordcount!".
Used within conditional statements to evaluate expressions.
=) [00:24:00][ "hello" = "hello" ]).-eq) [00:25:18][ 1 -eq 1 ]).-ne (not equal), -gt (greater than), -lt (less than), etc.$?) [00:24:39]$? stores the exit code of the last executed command.0 typically indicates successful execution.1) indicates an error or a false condition.
Control flow statements that execute code blocks based on conditions.
if [ condition ]; then commands ficommands if condition is true.elif [ condition ]; then commandsif or elif conditions were false.else commandscommands if none of the preceding if or elif conditions were true.fi (if spelled backward)."${1,,}" converts the first positional argument to lowercase for a case-insensitive comparison.An alternative to if/elif/else for checking multiple values, offering better readability for many conditions.
case $variable in pattern1) commands ;; pattern2) commands ;; esac;; marks the end of a pattern's commands.*) [00:30:11]| (e.g., pattern1 | pattern2)).*) acts as a catch-all or default option, similar to an else statement, executed if no other patterns match.Variables that can hold multiple values, indexed numerically.
MY_LIST=(item1 item2 item3).${MY_LIST[index]} (e.g., ${MY_LIST[<a href="https://youtube.com/watch?v=tK9Oc6AEnR4&t=0">0</a>]} for the first element).${MY_LIST[@]}.
Iterate over a list of items or a range of numbers.
for item in "${MY_LIST[@]}"; do commands; doneitem variable takes on each value from the array during iteration.-n flag with echo prevents newline characters from being counted by wc -c.
Reusable blocks of code within a script.
function_name() { commands; } or function function_name { commands; }local keyword (local VAR_NAME=value) to restrict a variable's scope to within the function.
$1, $2, etc., within the function's scope.Control the exit status of functions and scripts.
return values [00:42:17]return command explicitly sets the exit code for a function.return 0 indicates success, return 1 indicates failure (or any non-zero for specific error types).$? after its execution.A powerful text processing tool for filtering and manipulating structured data.
awk '{print $1}' filename.txt prints the first field (word) of each line, using space as the default separator.$0 refers to the entire line, $1 to the first field, $2 to the second, and so on.-F) [00:44:22]-F flag allows specifying a custom field separator (e.g., awk -F ',' '{print $1}' csvfile.csv uses a comma).echo "Hello:World" | awk -F ':' '{print $2}').
A stream editor used for transforming text, primarily for substitution.
s/old/new/g) [00:46:14]sed 's/pattern/replacement/flags' filename.txts: Substitute command.pattern: The text to find (can be a regular expression).replacement: The text to replace it with.g: Global flag, replaces all occurrences on a line (otherwise, only the first).
-i.ORIGINAL) [00:47:13]-i flag allows in-place editing of a file.-i.ORIGINAL) creates a backup of the original file before making changes.sed -i.ORIGINAL 's/fly/grasshopper/g' sedtest.txt