$_ CLI In Depth
Learning progress 0%
build terminal shell kernel

Build: A Command Explainer Script

6 min read Module 1: Terminal, Shell & Kernel Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

The specification

Write explain, a bash script in /workspace. It takes command names as arguments. For each name it prints what the shell would run: a kind from type -t. For files it also prints the full path from command -v and the output of file -L on that path. The script exits 0 if every name was found and 1 otherwise.

Here is the output format for explain cd ls printf [[ nosuchcmd. The file -L line is illustrative, and the exact text and path depend on your system.

TEXT6 lines
cd: builtin
ls: file /bin/ls
/bin/ls: ... (file -L output)
printf: builtin
[[: keyword
nosuchcmd: not found

A missing name is reported, not fatal. The script keeps going through the remaining arguments, and only the exit status records that something was missing.

Build it step by step

Start with the shebang and set -u

The first line is #!/usr/bin/env bash. The kernel reads the shebang and runs the interpreter it names. Using env finds bash through PATH, so the script works even when bash lives in a different place, such as a Homebrew prefix.

Next comes set -u. It makes any reference to an unset variable an error, so a typo such as $nmae stops the script instead of quietly expanding to nothing. Bash 5 does not complain about an empty "$@" under this option, so running the script with no arguments is safe.

Loop over the arguments

Write the loop as for name in "$@"; do. The double quotes matter. "$@" expands to one word per argument, and each argument stays intact even if it contains spaces. An unquoted $@ would split arguments again, and an argument containing a glob character such as * would be expanded against the files in the current directory.

Pass each name to type after --, which stops a name beginning with a hyphen from being read as an option. Keep the variable quoted as "$name" as well.

Capture the kind and branch on it

Capture the kind with kind=$(type -t -- "$name"). When a name is not found, type -t prints nothing and returns a nonzero status, so $kind ends up empty. An empty value is your signal for "not found". Command substitution also strips trailing newlines, so you do not need to clean up the output.

Dispatch on the kind with a case statement. The shape is this, and your version should fill in each branch:

TEXT5 lines
case $kind in
  '')   print "not found" and set status to 1
  file)  print the kind, the path, and file -L output
  *)     print the kind
esac

The empty pattern '' matches the not-found case. The file branch needs the path, so it calls command -v -- "$name". The catch-all * covers alias, keyword, function, and builtin, which print their kind unchanged.

Quote the file path

The file branch calls file -L "$path". The -L flag tells file to follow symbolic links, so you see what the link points to rather than the link itself. Quoting the path keeps a directory name with spaces from becoming two arguments.

Test it

Make the script executable, then run it on the names from the specification. The first run should exit 1 because nosuchcmd is not found. The second run should exit 0.

BASH5 lines
chmod +x explain
./explain cd ls printf "[[" nosuchcmd
echo "status=$?"
./explain cd ls printf
echo "status=$?"

Then try a name with a leading hyphen, such as ./explain -x. Because of the --, it should print -x: not found rather than an error from type.

Stretch goals

Show all definitions with type -a

Some names exist twice. echo is a builtin, and /bin/echo is also a program on disk. Running type -a echo prints both, in lookup order: the builtin first, then the file. Extend the script so that when the kind is builtin, it also runs type -a -- "$name" and prints the extra lines it reports.

Print the first line of help

For file names, "$path" --help 2>&1 | head -n 1 often shows a one-line usage summary. For builtins, help "$name" | head -n 1 prints the bash synopsis. Not every program supports --help, and some write it to stderr, so keep the redirection in place and handle empty output.

Why aliases are invisible to the script

Define an alias in your terminal, then ask about it:

BASH2 lines
alias ll="ls -l"
type -t ll

The terminal reports alias. Now run ./explain ll. It reports ll: not found, and the exit status is 1. The explanation is in how bash works. An alias lives in the memory of an interactive shell, and it is created by the line you type or by ~/.bashrc when the interactive shell starts.

When you run a script, bash starts a new, non-interactive process. That process does not read ~/.bashrc, and alias expansion is off by default in non-interactive shells. The script's type -t therefore looks in its own empty alias table. It reports on its own shell, not on the shell that launched it.

To show an alias definition from inside the script, the lookup needs a different source of truth. One option is to ask an interactive child shell with bash -i -c, which reads ~/.bashrc and so sees the alias. It is slower and may print startup output, so treat it as an exercise rather than a default.

Reference solution

This version meets the specification. Save it as /workspace/explain, make it executable, and compare it with your own version. Read each branch and check it against the behavior you saw in the tests.

BASH23 lines
#!/usr/bin/env bash
# explain: report what each command name resolves to.
set -u

status=0
for name in "$@"; do
  kind=$(type -t -- "$name")
  case $kind in
    '')
      echo "$name: not found"
      status=1
      ;;
    file)
      path=$(command -v -- "$name")
      echo "$name: file $path"
      file -L "$path"
      ;;
    *)
      echo "$name: $kind"
      ;;
  esac
done
exit "$status"

From keystrokes to a running program

Visualizer

Follow one line through the shell: read it, split it into words, expand them, find the command, run it, and collect its exit status.

1 / 8
read
tokenize
expand
look up
run
wait
$?
$ ls -l /tmp

Bash (through readline) reads one line from the terminal. Nothing has run yet: it is just the text ls -l /tmp.

type -a ls; ls -l /tmp; echo "exit status: $?"
Terminal lab · 3 steps · real Linux in your browser

Build the explain script

Write /workspace/explain, a script that reports what kind of command each name is.

01
An executable script
Create explain in /workspace with a #!/usr/bin/env bash shebang and make it executable with chmod +x explain.
Show hint for step 1
The first line must start with `#!`, and `ls -l explain` should show `x` bits.
02
Report the kind
For every name given, ./explain prints a line that includes the name and its kind from type -t. For files, include the path from command -v. Try ./explain cd ls printf '[['.
Show hint for step 2
Loop with `for name in "$@"`, and call `type -t "$name"` inside.
03
A meaningful exit status
./explain exits 0 when every name was found and 1 when any name was not. Try ./explain ls nosuchcmd; echo $?.
Show hint for step 3
Keep a variable such as `status=0`, set it to 1 when `type -t` finds nothing, and `exit "$status"` at the end.
Check yourself Question 1 of 4
Concept Check Read the type -t kind for a builtin recall

What does type -t printf print in bash 5 on Linux?

Recap & Key Takeaways

type -t reports one word per name: alias, keyword, function, builtin, file, or nothing when the name is not found.
Quote "$@" and "$name", and pass names to type and command after -- so unusual names behave correctly.
An empty result from type -t is the reliable signal that a name was not found, and it lets the script keep going.
Aliases are defined in an interactive shell and are not expanded in a non-interactive script, so a script cannot see your ~/.bashrc aliases by default.
Exit 0 only when every name resolves; a single miss makes the script exit 1.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue