Course outline · 0% complete

0/29 lessons0%

Course overview →

Your first script

lesson 8-1 · ~10 min · 21/29

Quiz

Warm-up combining lessons 6-1 and 7-1: why do you run your own script as ./deploy.sh instead of just deploy.sh, and what must you chmod first?

A script is just commands in a file

Scripts are the destination this whole course has been driving toward: every deploy pipeline, scheduled job, and "run this to set up the project" file you'll meet in industry is a shell script. The professional instinct: the second time you type the same five commands, put them in a file.

Everything you've typed so far, you can save and replay. A bash script is a plain text file of commands, run top to bottom. Three ingredients:

  1. The shebang. The first line #!/bin/bash tells the OS which program should interpret this file. (# starts a comment anywhere else, so use comments liberally.)
  2. Execute permission: chmod +x script.sh (lesson 6-1).
  3. Run it by path: ./script.sh (lesson 7-1 explained why the ./).

You can also skip steps 2-3 and run any script with bash script.sh. Both forms are everywhere in real projects.

Code exercise · bash

Run it. We build hello.sh out of echo lines (a script writing a script!), make it executable, and run it the professional way.

Code exercise · bash

Scripts get powerful the moment you combine them with unit 7's variables: define a value once at the top, use it everywhere below. Run it — changing the project name on ONE line would update both messages.

Quiz

What does the first line #!/bin/bash in a script do?

Code exercise · bash

Your turn. Build a script `goodbye.sh` (shebang plus two echo lines), chmod it, and run it with ./goodbye.sh. Expected output: ``` So long and thanks for all the fish ```