- Why do we need an exit status?
So commands can signal to their callers if they succeeded or failed in a simple manner.
- What is the difference between exit status, exit code and return code?
An exit code and return code refer to the same thing. An exit status is a concept, which is brought to life by the exit/return code.
- Which flag do we use with test to test for:
- An existing directory
-d
- A writable file
-w
- An existing symbolic link
-h (or -L)
- What is the preferred shorthand syntax for test -d /tmp/?
[[ -d /tmp/ ]]. Note that a space after [[ and before ]] is mandatory, or the command will fail!
- How can we print debug information in a Bash session?
Set the -x flag, either in the shell with set -x or when calling a script with bash -x.
- How can we check if a variable has content?
- if [[ -n ${variable} ]] to check if the variable is non-zero
- if [[ ! -z ${variable} ]] to check if the variable is not zero
- What is the Bash format for grabbing a return code?
$?.
- Of || and &&, which is the logical AND and which the OR?
|| is OR, && is AND.
- What is the Bash format for grabbing the number of arguments?
$#.
- How can we make sure it does not matter from which working directory the user calls the script?
By providing a cd $(dirname $0) at the beginning of the script.
- How do Bash parameter expansions help us when dealing with user input?
It allows us to remove capital letters so we can more easily compare to expected values.