Skip to the content.

🔗 Link to the Room

🔗 Link for further learning

🏷️ Table of Contents

  1. Our first simple bash scripts
  2. Variables
    2.1 Debugging Bash scripts
    2.2 Using multiple variables
  3. Parameters
    3.1 Command-line parameters
    3.2 Interactive input with read
    3.3 Special Parameters in Bash
  4. Arrays
    4.1 Indexes
    4.2 Creating an array in Bash
    4.3 Printing all items in an array
    4.4 Accessing a single item
    4.5 Removing an item from an array
    4.6 Changing (replacing) an item
  5. Conditionals
    5.1 if statement
    5.2 Common comparison operators
    5.3 Using parameters in conditionals
    5.4 Checking files with conditionals

📚 Study Notes

Our first simple bash scripts

  1. Every bash script needs a starting line
    • A bash script must start with this line: #!/bin/bash
    • This tells your system: “Run this file using Bash.”
    • Without it, the system won’t know how to execute your script.
  2. Your first example: printing text
    • You can make Bash print text to the terminal using echo.
    • Example: echo "Hello World"
    • When you run the script, it will display: Hello World
    • Think of echo like print in Python — it just shows text on the screen.
  3. You can run normal Linux commands in scripts
    • Any command you can run in the terminal can also go inside a bash script.
    • For example: ls
    • When the script runs, it will list the files in the current directory — just like typing ls directly into the terminal.
    • You can even combine commands, like whoami or id = This will show your current username and your user and group IDs
  4. Making the script runnable
    • Before you can run your script, you must give it permission: chmod +x yourfile.sh
    • Then run it with ./yourfile.sh
    • The ./ means “run this file from the current directory.”

❓What piece of code can we insert at the start of a line to comment out our code?#

❓What will the following script output to the screen, echo “BishBashBosh”BishBashBosh

Variables

image

image

Debugging Bash scripts

image

Using multiple variables

image


❓What would this code return?Jammy is 21 years old

❓How would you print out the city to the screen?echo $city

❓How would you print out the country to the screen?echo $country

Parameters

Command-line parameters

image

Interactive input with read

image

Special Parameters in Bash

Parameter **Description Usage
$0 tells you which script is currently running writting usage messages; debugging; logging which script ran
$# tells you how many arguments were passed to the script useful for checking if the user passed enough arguments
$@ all arguments passed to the script, one by one commonly used in loops
$* similar to $@, but it treats everything as one big string recommended to avoid it unless you know why you need it
$? exit status of last command, if either succeeded or failed for error handling, automation, security scripts
$$ gives the PID of the running script for logging, temporary files, process tracking

❓How can we get the number of arguments supplied to a script?$#

❓How can we get the filename of our current script(aka our first argument)?$0

❓How can we get the 4th argument supplied to the script?$4

❓If a script asks us for input how can we direct our input into a variable called ‘test’ using “read”read test

❓What will the output of “echo $1 $3” if the script was ran with “./script.sh hello hola aloha”hello aloha

Arrays

Indexes

image

Creating an array in Bash

Printing all items in an array

Accessing a single item

Removing an item from an array

Changing (replacing) an item

Arrays are great when you need to store multiple values of the same type, loop through data, organize inputs
cleanly, build more advanced scripts (automation, security checks, etc.)

Extend your biography maker:

 


❓What would be the command to print audi to the screen using indexing.echo "${cars[1]}"

❓If we wanted to remove tesla from the array how would we do so?unset cars[3]

❓How could we insert a new value called toyota to replace tesla?cars[3]='toyota'

Conditionals

if statement

image

Example:
image

What’s happening:

Common comparison operators

operator definition
-eq equal to
-ne not equal to
-gt greater than
-lt less than
-ge greater than or equal to

Using parameters in conditionals

image

Checking files with conditionals

image


❓What is the flag to check if we have read access to a file?-r

❓What is the flag to check to see if it's a directory?-d