Bash Basics

Lesson 8 of 8

The Survival Manual

Concept:

A bash script is a file containing a sequence of commands, one per line.

#!/bin/bash โ€” the shebang. First line of every script. Tells the system to use bash.
Variables โ€” store values: NAME="world". Retrieve with $NAME.
read โ€” gets input from the user.
echo โ€” prints output.

Scripts combine everything you've learned โ€” file ops, pipes, text processing โ€” into reusable programs. Save as .sh, make executable with chmod +x, run with ./script.sh.

Terminal: You've mastered individual tools โ€” navigation, files, search, pipes, text processing. Now it's time to combine them into something permanent: a script. A survival manual you can run again and again.
You: A script? Like programming?
Terminal: The simplest kind. A bash script is just a text file with commands, one per line. Start with '#!/bin/bash' โ€” that's the shebang. It tells the system: 'this file speaks bash.' After that, every line runs in order, just like typing commands one by one.
You: Can it remember things? Like a name?
Terminal: Variables. 'NAME="base camp"' stores a value. '$NAME' retrieves it. 'read ANSWER' waits for your input. You can build interactive tools โ€” a script that asks what you need and builds it for you.
You: So I could automate setting up camp every time?
Terminal: Exactly. One script that creates directories, generates files, writes content โ€” your entire camp setup in one command. Every great survivor automates the repetitive tasks. Write a script that builds a small website structure. Prove you can automate survival.
Example Code:
#!/bin/bash

# Ask the user for a project name
echo "Enter project name:"
read PROJ_NAME

# Create the directory and enter it
mkdir $PROJ_NAME
cd $PROJ_NAME

# Create files
touch index.html style.css
echo "<h1>Hello from $PROJ_NAME</h1>" > index.html

# List the result to show it worked
echo "Project $PROJ_NAME created successfully!"
ls -F

Your Assignment

Write a bash script that: 1) Creates a directory called 'mysite', 2) Creates 'index.html' and 'style.css' inside it, 3) Writes '<h1>Hello World</h1>' into index.html, 4) Lists the contents of mysite/, 5) Prints 'Done!' when finished. Don't forget the #!/bin/bash shebang!

Bash Console