Quiz: Bash & Shell Mastery
Can you talk to computers? Like, well?
Test your Bash scripting skills with these 16 questions!
Covering variables, loops, conditionals, string manipulation, functions and basic-to-tricky syntax gotchas.
Sharpen (or prove) your shell scripting skills!
How are variables defined in Bash?
Variables in Bash are declared without spaces around the = sign. For example:
name=AliceThis assigns the value "Alice" to the variable name.
Note: $name is used to reference or read a variable’s value.
Adding spaces causes the shell to interpret the command as a program to run, which is not what you want when setting a variable.
Also, Bash is case-sensitive, so name, NAME and Name are different variables.
Finally, variables can’t have spaces or dashes (-) in their names. Use underscores (_) or camelCase instead.
What will print out It's 🔨 Time!?
I know. It’s wild how quickly escaping makes strings tough to parse. Imagine escaping other languages in Bash strings - with all those quotes, apostrophes and $ symbols to f*ck you up. 🫠
A single quote cannot appear inside a Bash single-quoted string, even with a backslash. Close the quoted segment, write an escaped quote outside it, then reopen the quoted segment. The close-quote, escaped-quote, reopen-quote sequence ('\'') allows output of:
It's 🔨 Time!There are other ways to handle this, but this is the most common.
Now, what will this print?
price="$100"echo "Cost: $price"Numbered variables have special meaning. In this case, $1 is a special variable that holds the first argument passed to the script or function.
Since we’re running the script in a REPL, there are no arguments, so $1 is empty. The remaining text 00 is printed as-is.
To print a literal $ character, use single quotes, or escape it with a backslash (\):
price="\$100"echo "Cost: $price"How can you get the length of a variable in Bash?
The ${#username} syntax returns the length of username.
For example:
username="@justsml"echo ${#username} # => 8While wc would work, it’s technically not a part of Bash.
The utility wc is an old inside joke referring to “water closet,” or toilet.
KIDDING! Does anyone reads these?
In reality wc is an ancient command from Posix (and the AT&T Unix days.) It is short for “word count” and it can count lines, words, and characters in a file or input stream.
What does this script output if the file cats.txt EXISTS?
if [ -e cats.txt]; then echo "File exists"else echo "File does not exist"fiDid you catch the missing space before the closing bracket?
Bash is quite touchy here: spaces are required inside bracket expressions.
Because the missing space makes the [ command see no closing ], Bash prints a diagnostic, treats the test as failed, and continues to the else branch.
The correct syntax is:
if [ -e example.txt ]; then echo "File exists"else echo "File does not exist"fiNote: Double brackets [[ ]] are recommended for conditional expressions. See BashFAQ.
How can we compare strings in Bash?
cat1="Rosie"cat2="Sunflower"if [ "$cat1" === "$cat2" ]; then echo "Same cat"else echo "Different cats"fiAnother test syntax error!
Did you catch the invalid === operator?
You might have been thinking of JavaScript…
With [ ... ], Bash reports a diagnostic and the condition is false, so the else branch prints Different cats. In Bash, use = or == for equality comparisons.
What will this script output?
function greet () { echo "$1"}greet Hi DanFunctions in Bash can accept arguments. The $1 variable holds the first argument passed to the function.
Remember, $0 is the script name, $1 is the first argument, $2 is the second, and so on. Spaces separate arguments. So, greet Hi Dan passes "Hi" as the first argument. In order to pass "Hi Dan" as a single argument, you would need to quote it: greet "Hi Dan".
How does math work in Bash?
Which of these correctly multiplies 10 and 0.5, printing 5?
What does the : do in this script?
rosie="Bad cat, good cat"echo ${rosie:9}The ${var:offset} syntax extracts a substring starting from offset. Here, the output is:
good catTo extract a substring of a specific length, use ${var:offset:length}.
To extract from the end of the string, use ${var: -offset}. (Note the space before the -!)
Did my Bash Quiz leave you in shambles?
Let me know in the comments below!
Further Reading
Brush up on your Bash skills with the following resources:
- Bash Guide
- BashFAQ
- ShellCheck
- Bash Academy
- Bash Scripting Tutorial
- Bash Reference Manual
- Bash Hackers Wiki
- Bash Guide for Beginners
- Bash Reference Card
