Typorithm · medium

Arguments & Exit Codes

bash-argsbashshebangsetexit
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Arguments & Exit Codes

0%

Scripting

The shebang picks the interpreter; set -euo pipefail makes the script strict (exit on error, error on unset vars, fail on any pipeline stage). $0 is the script name, $1.. the args, $# the count, $@ all of them; exit sets the script's status code.

The first line, the shebang (#!/usr/bin/env bash), tells the system which interpreter should run the file. Using env finds bash on the PATH.

set -euo pipefail turns on strict mode: -e exits on any failing command, -u treats an unset variable as an error instead of an empty string, and -o pipefail makes a pipeline fail if any stage in it fails (not just the last).

Inside the script, $0 is the script's own name, $1, $2, ... are the arguments, $# is how many arguments there are, and $@ is all of them. exit sets the script's status code — 0 means success and any non-zero value, like exit 1, signals failure.

01

Shebang #!/usr/bin/env bash picks the interpreter.

02

set -euo pipefail = exit on error, error on unset, fail any pipeline stage.

03

$0 name, $1.. args, $# count, $@ all; exit sets the status code.

#!/usr/bin/env bash
set -euo pipefail
echo "script: $0 first: $1"
echo "count: $#"
exit 1

Read the implementation

Connect each part of the code to the concept before you type.

1. Shebang

The shebang line tells the OS which interpreter to run the script with — here bash via env.

#!/usr/bin/env bash

2. Strict mode

-e exits on any error, -u errors on unset variables, -o pipefail fails a pipeline if any stage fails.

set -euo pipefail

3. Name & first arg

$0 is the script's name and $1 is its first command-line argument.

echo "script: $0 first: $1"

4. Argument count

$# expands to the number of positional arguments passed to the script.

echo "count: $#"

5. Exit code

exit ends the script and sets its status code; non-zero like 1 signals failure.

exit 1

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Arguments & Exit Codes: Lesson & Practice · Typorithm | ApplyDjinn