Bash quiz – 40 questions with answers
Try it interactively
With scoring, random questions, and saved progress – or go straight to the timed exam with certificate.
Beginner (13 questions)
What does the command pwd display?
- The user's password
- The path of the current directory
- All running processes
- The version of the shell
Show answer
Correct: The path of the current directory
pwd stands for "print working directory" and prints the absolute path of the current folder.
What does this command print?
echo "a b c" | wc -w- 1
- 3
- 5
- 6
Show answer
Correct: 3
wc -w counts words (separated by spaces): a, b, c = 3.
What does this code print?
x=5
echo $x+1- 6
- 5+1
- x+1
- Error
Show answer
Correct: 5+1
Without $(( )) Bash does not calculate: $x is replaced by 5 and +1 stays text – output 5+1.
What does this code print?
echo $((7 / 2))- 3.5
- 3
- 4
- Error
Show answer
Correct: 3
Arithmetic in Bash is integer-only: 7 / 2 = 3 (the remainder is discarded).
What happens with this assignment?
name = "Ada"- name gets the value Ada
- Error: "name: command not found"
- name becomes empty
- name becomes a constant
Show answer
Correct: Error: "name: command not found"
With spaces around =, Bash tries to start a program called name. Correct: name="Ada".
What does this command print?
name="Ada"
echo 'Hello $name'- Hello Ada
- Hello $name
- Hello
- Error
Show answer
Correct: Hello $name
Nothing is expanded inside single quotes – the text stays literal. With double quotes, Hello Ada would appear.
What is the difference between > and >>?
- No difference
>overwrites the file,>>appends>appends,>>overwrites>>redirects stderr
Show answer
Correct: > overwrites the file, >> appends
> creates the file anew (content is lost), >> adds to the end.
What does [[ -f datei.txt ]] check?
- Whether
datei.txtis a folder - Whether
datei.txtexists as a regular file - Whether
datei.txtis empty - Whether
datei.txtis executable
Show answer
Correct: Whether datei.txt exists as a regular file
-f tests for a regular file. -d checks folders, -s "not empty", -x "executable".
What does cd - do?
- Changes to the home directory
- Changes to the previous directory
- Goes up one level
- Shows the current path
Show answer
Correct: Changes to the previous directory
cd - jumps back to the most recently visited folder (variable $OLDPWD).
What value does $? have right after a successful command?
- 1
- 0
- -1
- true
Show answer
Correct: 0
Exit code 0 means success, any other value an error.
What is chmod +x skript.sh for?
- It deletes the script
- It makes the file executable
- It runs the script
- It encrypts the script
Show answer
Correct: It makes the file executable
Without the execute permission you cannot start ./skript.sh directly (only bash skript.sh).
What does this code print if the variable gast is not set?
echo "${gast:-Guest}"- Nothing
- Guest
- ${gast:-Guest}
- Error
Show answer
Correct: Guest
${var:-x} returns x if var is unset or empty.
What does this line count?
ls | wc -l- Characters in the current folder
- Entries in the current folder
- Running processes
- Lines of a file
Show answer
Correct: Entries in the current folder
ls prints one entry per line (when output goes to a pipe), wc -l counts the lines.
Intermediate (14 questions)
What distinguishes "$@" from "$*"?
- No difference
"$@"returns each argument as its own word,"$*"glues all into one string"$*"returns each argument individually,"$@"does not"$@"also contains the script name
Show answer
Correct: "$@" returns each argument as its own word, "$*" glues all into one string
"$@" keeps arguments with spaces intact – almost always the right choice.
What does this code print?
set -- a "b c" d
echo $#- 3
- 4
- 2
- 5
Show answer
Correct: 3
set -- sets the positional arguments: a, b c (one argument, since it is in quotes) and d. $# = 3.
What does this code print?
[[ 10 > 9 ]] && echo yes || echo no- yes
- no
- Error
- Nothing
Show answer
Correct: no
Inside [[ ]], > compares as text: "10" is lexicographically smaller than "9". For numbers: -gt or (( 10 > 9 )).
What does this code print?
[[ "abc" == a* ]] && echo yes || echo no- yes
- no
- Error
- a*
Show answer
Correct: yes
To the right of == inside [[ ]], a pattern applies if it is not in quotes: a* matches abc.
What does this query return for this test content? (server.log with 6 lines, 2 of them with ERROR)
grep -c ERROR server.log- 6
- 2
- 4
- ERROR
Show answer
Correct: 2
-c prints the number of matching lines: 2.
What does this command return for daten.csv (header line, followed by the ages 36, 25 and 41)?
awk -F, 'NR > 1 { s += $3 } END { print s }' daten.csv- 102
- 36
- 3
- 0
Show answer
Correct: 102
NR > 1 skips the header line; the sum 36 + 25 + 41 is 102.
What does this command print?
echo "banana" | sed 's/a/X/g'- bXnana
- bXnXnX
- bananX
- XXXXXX
Show answer
Correct: bXnXnX
The g flag replaces all matches; without g only the first would be replaced (bXnana).
What does this command print?
printf 'b\na\nb\n' | sort | uniq -c- 1 a and 2 b (with a count before each line)
- 2 a and 1 b
- a and b (without counts)
- 3
Show answer
Correct: 1 a and 2 b (with a count before each line)
Sorted, this gives a, b, b; uniq -c merges identical adjacent lines and counts: 1 a, 2 b.
How do you correctly handle file names with spaces in find and xargs?
find ... | xargs cmdfind ... -print0 | xargs -0 cmdfind ... | tr " " "_" | xargs cmd- Not possible at all
Show answer
Correct: find ... -print0 | xargs -0 cmd
The NUL character (-print0 / -0) is the only separator that cannot occur in file names.
What does return deliver in a Bash function?
- Arbitrary text
- An exit code between 0 and 255
- A number of any size
- An array
Show answer
Correct: An exit code between 0 and 255
return only sets the exit status. For values, use echo and $(function).
What does this code print?
f() { echo "$1-$2"; }
f a- a-
- a-a
- Error
- -
Show answer
Correct: a-
$2 is not set and yields an empty string (without set -u): output a-.
What does set -u do?
- Aborts on every error
- Unset variables are an error
- Enables debug output
- Makes variables immutable
Show answer
Correct: Unset variables are an error
set -u (nounset) catches typos in variable names. -e aborts on errors, -x debugs, readonly locks variables.
What does this code print?
echo $((2 ** 10))- 20
- 1024
- 100
- Error
Show answer
Correct: 1024
** is the exponentiation operator of Bash arithmetic: 2¹⁰ = 1024.
Which statement about cmd1 && cmd2 || cmd3 is true?
- cmd3 runs only if cmd1 fails
- cmd3 runs if cmd1 OR cmd2 fails
- cmd3 runs only if cmd2 fails
- cmd3 always runs
Show answer
Correct: cmd3 runs if cmd1 OR cmd2 fails
It is not an if/else: if cmd2 fails, cmd3 runs too. For a real if/else use if ...; then ...; else ...; fi.
Advanced (13 questions)
What is set -o pipefail for?
- Speeds up pipes
- The exit code of a pipeline is that of the last failed command
- Forbids pipes
- Sends stderr into the pipe
Show answer
Correct: The exit code of a pipeline is that of the last failed command
Without pipefail only the exit code of the last command counts; a failure at the start would go unnoticed.
What does trap 'rm -f "$tmp"' EXIT do?
- Deletes
$tmpimmediately - Deletes
$tmpwhen the script ends – even on error or Ctrl+C - Prevents the script from ending
- Deletes
$tmponly on success
Show answer
Correct: Deletes $tmp when the script ends – even on error or Ctrl+C
The EXIT trap runs when the shell exits, no matter how. Ideal for cleanup work.
What output is produced?
pfad="/home/ada/bericht.tar.gz"
echo "${pfad##*/}"- bericht.tar.gz
- /home/ada
- bericht
- gz
Show answer
Correct: bericht.tar.gz
##*/ removes the longest match of */ at the beginning – what remains is the file name (like basename).
What output is produced?
datei="bericht.tar.gz"
echo "${datei%.gz}"- bericht.tar
- bericht
- tar.gz
- bericht.tar.gz
Show answer
Correct: bericht.tar
% removes the shortest match at the end. %%.* would remove the longest (bericht).
What does this code print?
arr=(a "b c" d)
echo "${#arr[@]}"- 3
- 4
- 5
- 1
Show answer
Correct: 3
The array has three elements – "b c" is a single one because of the quotes. ${#arr[@]} counts elements, not characters.
What does this script print?
zaehler=0
printf 'a\nb\nc\n' | while read -r x; do
zaehler=$((zaehler + 1))
done
echo "$zaehler"- 3
- 0
- 1
- Error
Show answer
Correct: 0
The pipe starts the loop in a subshell; the change is lost afterwards. Solution: done < <(printf ...) or redirection from a file.
What does this code print?
echo $(( 010 ))- 10
- 8
- Error
- 1
Show answer
Correct: 8
A leading 0 denotes an octal number in Bash arithmetic: 010 = 8. For decimal use 10#010.
What exit code does echo $? show here?
f() {
local v=$(false)
echo $?
}
f- 1
- 0
- 127
- It is a syntax error
Show answer
Correct: 0
local itself returns exit code 0 and masks the one from false. Better: local v; v=$(false).
What does the cron expression */15 * * * 1-5 mean?
- Every 15 hours on every day
- Every 15 minutes from Monday to Friday
- At 3 PM on weekdays
- Every 15 days of the month
Show answer
Correct: Every 15 minutes from Monday to Friday
Field 1 (*/15) = every 15 minutes, field 5 (1-5) = Monday to Friday.
What does <<'EOF' (end word in single quotes) do in a here-doc?
- The text is executed as a command
- Variables and
$(...)in the text are not expanded - The text is encrypted
- The text is capitalized
Show answer
Correct: Variables and $(...) in the text are not expanded
With a quoted end word the text stays literal – ideal for writing scripts or configurations containing $ into files.
What does diff <(sort a) <(sort b) do?
- It permanently sorts both files
- It compares the sorted outputs without creating intermediate files
- It merges both files
- It opens two terminals
Show answer
Correct: It compares the sorted outputs without creating intermediate files
Process substitution provides the output of each command as a temporary file name.
Why are associative arrays (declare -A) missing in the macOS system Bash?
- They are deprecated
- For licensing reasons macOS still ships Bash 3.2; they exist from Bash 4
- They are only allowed in zsh
- They require root permissions
Show answer
Correct: For licensing reasons macOS still ships Bash 3.2; they exist from Bash 4
Apple ships Bash 3.2 (GPLv2). Newer Bash versions can be installed via Homebrew.
How do you safely run user input as a command?
eval "grep $eingabe datei"befehl=(grep -- "$eingabe" datei); "${befehl[@]}"bash -c "grep $eingabe datei"echo $eingabe | sh
Show answer
Correct: befehl=(grep -- "$eingabe" datei); "${befehl[@]}"
An array passes each argument separately, without the shell re-interpreting the content. eval and bash -c with user input are command injection.