Skip to main content

Command Palette

Search for a command to run...

Learning Bash Scripting: Variables, Parameters, Arrays, and Conditionals

Published
4 min read

As part of my cybersecurity and scripting learning journey, I’ve been building small Bash scripts to practice variables, parameters, arrays, and conditionals. This post documents my notes and progress. It’s a part of my ongoing journey into Bash scripting. I’m documenting the fundamentals: variables, parameters, arrays, and conditionals - not just to reinforce my own learning, but also to share a clear reference I can revisit in the future. When diving into Bash scripting, you’ll quickly realize it’s both powerful and quirky. Let’s walk through the essentials I’ve been learning step by step.

Variables in Bash

Variables let us store and reuse data. This snippet shows how to use multiple variables inside a single echo statement:

name="Jammy"
age=21
echo "$name is $age years old"

Output:

Jammy is 21 years old

You can also store other values:

city="Paris"
country="France"

echo $city
echo $country

Parameters

When you run a script, you can pass values directly from the command line.

#!/bin/bash
name=$1
echo $name

Run it like this:

./example.sh Alex

Output:

Alex
  • $1 → the first argument

  • $2 → the second argument

  • $# → number of arguments

  • $0 → the script name itself

Example:

echo $1 $3

If run with:

./script.sh hello hola aloha

Output:

hello aloha

You can also combine parameters with read for interactivity:

#!/bin/bash
echo "Enter your name"
read name
echo "Your name is $name"

Arrays in Bash

Arrays store multiple values.

transport=('car' 'train' 'bike' 'bus')
  • Print everything:
echo "${transport[@]}"
  • Print specific index:
echo "${transport[1]}"

Output:

train
  • Replace a value:
transport[1]='trainride'
  • Remove a value:
unset transport[1]

Another example:

cars=('honda' 'audi' 'bmw' 'tesla')
echo "${cars[1]}"   # prints audi
unset cars[3]       # removes tesla
cars[3]='toyota'    # replaces with toyota

Conditionals

Conditionals allow your script to make decisions.

Basic syntax:

if [ condition ]
then
    # do something
else
    # do something else
fi

Example:

count=10

if [ $count -eq 10 ]
then
    echo "true"
else
    echo "false"
fi

Operators

  • -eq → equal

  • -ne → not equal

  • -gt → greater than

  • -lt → less than

  • -ge → greater than or equal

String Comparisons

value="guessme"
guess=$1

if [ "$value" = "$guess" ]
then
    echo "They are equal"
else
    echo "They are not equal"
fi

Run with:

./example.sh guessme

Output:

They are equal

File Tests

Flags for file checks:

  • -f → file exists

  • -w → writable

  • -r → readable

  • -d → directory

Example:

filename=$1

if [ -f "$filename" ] && [ -w "$filename" ]
then
    echo "hello" > $filename
else
    touch "$filename"
    echo "hello" > $filename
fi

Run with:

./example.sh hello.txt
cat hello.txt

Output:

hello

Example: Simple Age Checker

#!/bin/bash
echo "How old are you?"
read age

if (( age >= 18 ))
then
    echo "You are eligible for work!"
else
    echo "You are ineligible for work!"
fi

Practice Projects (Next Steps)

Here are some practice projects I plan to work on with Bash scripting:

  • Biography Maker: Write a script that asks for a name, age, and job, then prints a short sentence combining them.

  • Tools List: Store a list of tools in an array and loop through them, printing each tool to the screen.

  • Guessing Game: Create a guessing game where the user input is compared against a stored value.

  • File Manager: Write a script that checks if a file exists. If not, it creates it, changes permissions, and writes some text to it.

Key Takeaways

  • Learned how to use variables and parameters to make scripts dynamic.

  • Understood the difference between command-line arguments ($1, $2) and interactive input (read).

  • Practiced arrays and indexing, which are powerful for handling multiple values.

  • Explored conditionals and file tests, useful for automation and checks in real-world scripting.

Wrapping Up

So far I’ve learned:

  • How to use variables for storing values

  • How to pass and handle parameters with $1, $2, $#

  • How to use arrays with indexing, unset, and replacement

  • How to write conditionals with if/else, numbers, and strings

  • How to check files and permissions using -f, -w, -r, -d

Bash can be tricky with its brackets, spaces, and operators, but once you get the hang of it, you can build interactive and useful scripts. This blog is part of my ongoing portfolio where I document everything I learn in Bash, Linux, and cybersecurity. My goal is to keep practicing, building scripts, and showing employers my progression.