CLI
Example
rm -rf $BUILD_DIR/rm -rf -- "${BUILD_DIR:?}/"Quotes stop word splitting, and :? aborts the command when the variable is empty instead of deleting from the root.
Common mistakes
- An empty variable turns
rm -rf $DIR/intorm -rf /. - A path with spaces is split into two arguments because it was not quoted.
Key points
- Shell variables in commands are quoted, as in
"$DIR", so spaces and empty values do not break them. - Destructive commands like
rm -rfguard against empty paths, for example with${DIR:?}. - Scripts exit with a non-zero code on failure, for example with
set -e.
Interview questions
Why use a CLI instead of a GUI?
Commands can be repeated, combined with pipes, saved in scripts and run on servers without a screen.
What is the difference between a CLI and a terminal?
A CLI is any text command interface, including one a program provides. The terminal is the app where you type those commands.