title: Bash Scripting Tutorial for Beginners
Youtube_Thumbnail: https://img.youtube.com/vi/tK9Oc6AEnR4/hqdefault.jpg
sources:
- https://www.youtube.com/watch?v=tK9Oc6AEnR4
media_link: https://www.youtube.com/watch?v=tK9Oc6AEnR4
Authors: "[[freeCodeCamp.org]]"
contentPublished: 2023-04-11
noteCreated: 2026-01-03
description: Learn bash scripting in this crash course for beginners. Understanding how to use bash scripting will enhance your productivity by automating tasks, streamli...
tags:
- clippings
- video
takeaways:
subjects:
- "[[Scripting Languages]]"
- "[[bash]]"
Status: ā
ReadLearn bash scripting in this crash course for beginners. Understanding how to use bash scripting will enhance your productivity by automating tasks, streamli...
echo Hello displays "Hello"vim filename.txt to open editori to enter insert modeEscape then :wq to write and quitEscape then :q! to quit without savingcat filename.txt displays file contents to terminal.sh extension: vim shell_test.sha key for text after cursor#!/bin/bash at the top of script#! is the shebang markerchmod u+x script.sh to give execute permissionsu+x grants only user (owner) permission./script.shfirst_name=Herbertecho Hello $first_nameread command to prompt for inputecho "What is your name?"read first_name./script.sh John Doe$1 (first arg), $2 (second arg), etc.$0 is reserved for the shell itselfecho Hello $1 $2| ls -l /usr/bin | grep bash> (greater than): Overwrites file
echo "hello world" > hello.txt>> (double greater than): Appends to file
echo "goodbye" >> hello.txt< (less than): Read file as input
wc -w < hello.txt<< (double less than): Heredoc syntax for multiline input
cat << EOF followed by text, then EOF<<< (triple less than): Single string input
wc -w <<< "Hello world"test command or use square brackets: [ expression ][ and before ]$? returns exit code of last command (0 = success)= operator: Check if strings equal[ "hello" = "hello" ] returns 0 (true)${var,,} to ignore case: [ "${1,,}" = "username" ]= or -eq for numeric equality-eq is safer for numbers (avoids alphabetical characters)[ 1 -eq 1 ] returns 0 (true)if [ test ]; then action; elif [ test ]; then action; else action; fiif starts conditional blockthen specifies action if test trueelif tests additional conditionselse default action if no conditions truefi ends block (reversed "if")case $variable in option1) action;; option2) action;; *) default;; esac| (pipe) to combine multiple options in one case: option1|option2) action;;* is catch-all/default option (like else);; (double semicolon)esac (reversed "case")my_array=(1 2 3 4 5)${my_array[0]} (first element, index 0)${my_array[@]}for item in ${array[@]}; do action; doneitem is loop variable representing current element${array[@]} iterates over entire arrayecho -n to suppress newlines when counting charactersdone
38:25 function_name() { commands; }function_namevar=$(command)local keyword to restrict scope: local var=valuemy_function $1 $2$1, $2 inside function$? (0 = success, 1 = failure)return statement to set exit codeawk '{print $1}' filename (prints first field)$1 = first item, $2 = second item, etc.awk '{print $1}' test.txt-F flag to specify field separatorawk -F, '{print $1}' test.csv-F, sets comma as delimiterecho "text:hello" | awk -F: '{print $2}'sed 's/find/replace/g' filenames = substitute mode/ separates mode from search term/ separates search from replacementg = global (replace all occurrences)-i flag with suffix: sed -i.original 's/old/new/g' filename.original extension-i and suffix