getbetterat.work
Complete Guide

Make Your Computer Do The Hard Work.

Learn Bash Scripting. It is a tool that lets you write lists of instructions for your computer. Save time, stop doing boring tasks, and become a master of your machine.

$ echo "Hello, Human."

Hello, Human.

$ start_automation --fast

Working... 100% Done in 0.1 seconds.

_

What Exactly is Bash?

Imagine you have a robot servant living inside your computer. You can tell this robot to move files, start programs, or check if the computer is healthy.

Bash is the language you use to talk to this robot.

Usually, you point and click with your mouse to do things. That is slow. With Bash, you type out your commands. It is much faster. When you put a lot of commands in a single text file and tell the computer to run them all at once, that is called a Script.

Mouse Clicks = Slow

Bash Commands = Instant

The 4 Big Jobs of Bash

1. File Management

Copying, moving, renaming, or deleting thousands of files takes seconds, not hours.

2. Process Control

Starting, stopping, and watching computer programs to make sure they do not freeze or crash.

3. System Maintenance

Cleaning out old trash files and making sure the hard drive has enough free space to work.

4. Automation Magic

Setting up a schedule so the computer does all the chores by itself while you sleep.

The Basic Words (Commands)

Before you write a full script, you need to know the basic words the computer understands. Type these into your terminal.

ls
List out things. It shows you all the files and folders in the room you are currently in.
cd
Change door. It moves you into a different folder on your computer.
mkdir
Make directory. It creates a brand new, empty folder.
rm
Remove. It deletes a file forever. Be very careful with this word!

Writing Your First Script

A script is just a normal text file filled with commands. Instead of typing commands one by one, the computer reads the file from top to bottom and does them all for you.

setup.sh
1
#!/bin/bash
The "Nametag". It tells the computer this file is a Bash script.
2
echo "Hello! I am going to build a folder for you."
"Echo" means to print words on the screen so you can read them.
3
mkdir MyNewFolder
4
cd MyNewFolder
5
echo "All done!"

Variables: Boxes of Information

A Variable is like a cardboard box with a name written on it. You can put information inside the box to use later.

If you want to say hello to a user, you put their name in a box called NAME. Then, you tell the script to read what is inside the box.

NAME="Sarah"

echo "Welcome, $NAME!"

NAME
"Sarah"
Call it using $NAME

If / Then Logic: Making Choices

You must teach the computer how to think. You do this by making rules. "IF this happens, THEN do that."

if [ file_exists ]

Does the secret file exist?

YES (Then)
echo "Read File"
NO (Else)
echo "Error! Missing!"

Loops: Doing Things Over and Over

The Problem

You have 100 images. You need to add ".old" to the end of every single name. Doing this by hand will take an hour.

The Solution: "For Loop"

Tell the computer to do the exact same action to every file in the folder. It finishes in 1 second.

# Look at every image one by one
for image in *.jpg;
do
mv "$image" "$image.old"
done
100 files changed instantly.

Permissions: The Bouncer

Your computer has rules about who can look at or change files. You use the command chmod to change these locks.

Read (r)

You are allowed to open the file and look at the text inside.

Write (w)

You are allowed to edit the text or delete the file.

Execute (x)

You are allowed to run the file as a program or script.

Piping: Connecting The Dots

Sometimes, one command is not enough. A Pipe (the | symbol) takes the answer from the first command and hands it directly to the next command.

ls -l
Get the list of files
|
sort
Organize them nicely

Redirection: Saving the Output

Normally, commands print their answers on your screen. You can use the > symbol to throw that answer into a text file instead.

# This creates a brand new file

echo "Hello World" > diary.txt

# This adds a new line to the bottom

echo "Day 2" >> diary.txt

diary.txt

Hello World
Day 2

Grep: The Super Search

If you have a file with 10,000 lines, finding one word is hard. Grep is a search engine for your terminal.

# Find the word "ERROR" in the giant log file grep "ERROR" server_logs.txt

It instantly prints only the lines that match. Magic.

Cron: The Alarm Clock

Scripts are great, but having to press "Start" every time is annoying. Cron is a tool that runs your scripts on a schedule.

You can tell Cron: Run backup.sh every day at 3:00 AM. Then, you can completely forget about it.

Minute - Hour - Day - Month - DayOfWeek
0 3 * * *
/path/to/backup.sh

Aliases: Making Shortcuts

If you type a long, confusing command every single day, you can invent your own short word for it. This is called an Alias.

Instead of typing this nightmare:

git commit -m "update" && git push origin main

Just set an Alias and type:

pushcode

User Input: Asking Questions

Your scripts can stop and ask the human a question. You use the read command to wait for them to type an answer.

echo "What is your name?"
read USERNAME
echo "Hello, $USERNAME! Building your profile now..."
Terminal Output:
What is your name?
Dave _
Hello, Dave! Building your profile now...

Functions: Mini-Scripts

If you need to do the same 5 things in several places in your script, put them in a Function. It is like a mini-script inside your main script.

say_hello() {
echo "Welcome to the system."
date
}

# Now you can just call the function name anywhere
say_hello

Exit Codes: Did it work?

Every time a command finishes, it secretly leaves behind a number. This is the Exit Code (stored as $?).

If the number is 0, everything was perfect. If the number is 1 or higher, something broke.

Exit Code: 0
Exit Code: 1

Safety First: set -e

By default, Bash is reckless.

If line 2 of your script fails, Bash will ignore the error and keep running line 3 anyway. This can destroy your computer.

Always put this at the very top of your scripts:

set -e

This tells Bash: "If ANY command fails, STOP the script immediately."

Who uses Bash every day?

Bash is an essential tool for people who build and protect the internet. If you want these jobs, you must learn Bash.

System Administrator

The guardian of the computers. They use Bash scripts to add new users, update security software, and make sure the servers never turn off.

DevOps Engineer

The builder of automated factories. They use Bash to automatically test new software code and push it live to millions of users without human error.

Stop clicking. Start typing.

You now know the ideas behind Bash scripting. It is time to open your terminal and type your very first command.

echo "I am ready"