Shell scripting basics

A script is the commands you keep retyping, written into a file once. These questions cover the shebang that starts it, the quoting that keeps values intact, and the exit code that tells you whether it worked.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 What is the shebang, and what does a first line of #!/bin/sh do?
2 Why write "$name" rather than $name when the variable might hold a space?
3 What does $? hold after a command finishes?
4 You have written hello.sh. Why does ./hello.sh fail with a permission error before you run chmod +x hello.sh?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Run a script and read its exit code

This writes a three-line script that stores today's date in a variable and prints it, makes the file executable, runs it, then prints the exit code of the run.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

printf '%s\n' '#!/bin/sh' 'today=$(date +%F)' 'echo "backup for $today"' > ~/date.sh; chmod +x ~/date.sh; ~/date.sh; echo $?
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson