CodeTheRobot

Ubuntu Linux for Robotics · Lesson 18 of 96

Bash scripts: shebang, variables and arguments

Writing a script, #!/bin/bash, making it executable, variables, arguments and exit codes.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain bash scripts: shebang, variables and arguments in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A shell script is a text file of commands. The first line, #!/bin/bash (the shebang), tells Linux which interpreter runs it. Make it executable with chmod +x script.sh and run it with ./script.sh. Variables are assigned without spaces (name=value) and read with a dollar sign ($name); quote them as "$name" so spaces do not split them.

Arguments arrive as $1, $2, and $# is their count. Every command returns an exit code (0 = success); $? holds the last one. Start scripts with set -euo pipefail so they stop on the first error, an unset variable or a failed pipe instead of carrying on with bad data.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the bash scripts: shebang, variables and arguments commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

#!/bin/bash
# Run this file with bash.
set -euo pipefail
# Stop at the first error, on unset variables and on failed pipes.
name="${1:-world}"
# Use the first argument, or 'world' if none is given.
echo "Hello, $name"
# Print a greeting; quotes keep spaces inside the value.
echo "got $# argument(s)"
# Print how many arguments were passed.
exit 0
# Report success to the caller.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Build and run

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

sourcecompilerprogramBUILD AND RUN

Demo for Bash scripts: shebang, variables and arguments. Expected result: Hello, robot args: 1

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

d=$(mktemp -d)
cat > "$d/greet.sh" <<'EOF'
#!/bin/bash
set -euo pipefail
# TODO: use the first argument as name and print "Hello, <name>" then "args: <count>"
EOF
chmod +x "$d/greet.sh"
"$d/greet.sh" robot

Expected output

Hello, robot
args: 1
Stuck? Show the full solution (try it yourself first)
d=$(mktemp -d)
cat > "$d/greet.sh" <<'EOF'
#!/bin/bash
set -euo pipefail
name="$1"
echo "Hello, $name"
echo "args: $#"
EOF
chmod +x "$d/greet.sh"
"$d/greet.sh" robot

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Write greet.sh that takes a name argument, prints a greeting, prints an error to stderr and exits with code 1 when no argument is given. Make it executable and test both cases, checking $?.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.