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.
cd: builtin
ls: file /bin/ls
/bin/ls: ... (file -L output)
printf: builtin
[[: keyword
nosuchcmd: not foundA 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:
case $kind in
'') print "not found" and set status to 1
file) print the kind, the path, and file -L output
*) print the kind
esacThe 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.
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:
alias ll="ls -l"
type -t llThe 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.
#!/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"