From Commands to a Script

What this is. A shell script is a text file containing the commands you would have typed. Running it runs them in order. There is no new language to learn, only a few conventions.

`

#!/bin/bash

echo Building project

mkdir -p build

echo Done

`

The first line is the shebang, and it tells the system which interpreter should run this file. Without it, the machine has to guess.

Three steps, every time. Write the file. Make it executable with chmod +x. Run it with ./name.sh.

That middle step is the one people forget, and the error it produces is permission denied rather than anything about scripts, which is why module 10 came first.

✦ Comments are part of the script

Any line beginning with a hash is ignored, apart from the shebang. Use them. A script you wrote three months ago is a script written by a stranger, and the one thing that stranger needed to tell you is why this exists rather than what each line does.

SANKOFA SANDBOX
learner:~$

Build a script line by line with echo and redirects, then run it.

Try it → Run `echo '#!/bin/bash' > hello.sh`, then `echo 'echo Hello there' >> hello.sh`, then `chmod +x hello.sh`, then `./hello.sh`. Notice the single quotes, which stop the shell interpreting anything as you write it.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Arguments, Variables, and Decisions →
← Back