shBash

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?

  1. The user's password
  2. The path of the current directory
  3. All running processes
  4. 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. 1
  2. 3
  3. 5
  4. 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
  1. 6
  2. 5+1
  3. x+1
  4. 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))
  1. 3.5
  2. 3
  3. 4
  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"
  1. name gets the value Ada
  2. Error: "name: command not found"
  3. name becomes empty
  4. 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'
  1. Hello Ada
  2. Hello $name
  3. Hello
  4. 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 >>?

  1. No difference
  2. > overwrites the file, >> appends
  3. > appends, >> overwrites
  4. >> 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?

  1. Whether datei.txt is a folder
  2. Whether datei.txt exists as a regular file
  3. Whether datei.txt is empty
  4. Whether datei.txt is 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?

  1. Changes to the home directory
  2. Changes to the previous directory
  3. Goes up one level
  4. 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. 1
  2. 0
  3. -1
  4. true
Show answer

Correct: 0

Exit code 0 means success, any other value an error.

What is chmod +x skript.sh for?

  1. It deletes the script
  2. It makes the file executable
  3. It runs the script
  4. 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}"
  1. Nothing
  2. Guest
  3. ${gast:-Guest}
  4. Error
Show answer

Correct: Guest

${var:-x} returns x if var is unset or empty.

What does this line count?

ls | wc -l
  1. Characters in the current folder
  2. Entries in the current folder
  3. Running processes
  4. 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 "$*"?

  1. No difference
  2. "$@" returns each argument as its own word, "$*" glues all into one string
  3. "$*" returns each argument individually, "$@" does not
  4. "$@" 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 $#
  1. 3
  2. 4
  3. 2
  4. 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
  1. yes
  2. no
  3. Error
  4. 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
  1. yes
  2. no
  3. Error
  4. 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
  1. 6
  2. 2
  3. 4
  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
  1. 102
  2. 36
  3. 3
  4. 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'
  1. bXnana
  2. bXnXnX
  3. bananX
  4. 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. 1 a and 2 b (with a count before each line)
  2. 2 a and 1 b
  3. a and b (without counts)
  4. 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?

  1. find ... | xargs cmd
  2. find ... -print0 | xargs -0 cmd
  3. find ... | tr " " "_" | xargs cmd
  4. 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?

  1. Arbitrary text
  2. An exit code between 0 and 255
  3. A number of any size
  4. 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
  1. a-
  2. a-a
  3. Error
  4. -
Show answer

Correct: a-

$2 is not set and yields an empty string (without set -u): output a-.

What does set -u do?

  1. Aborts on every error
  2. Unset variables are an error
  3. Enables debug output
  4. 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))
  1. 20
  2. 1024
  3. 100
  4. Error
Show answer

Correct: 1024

** is the exponentiation operator of Bash arithmetic: 2¹⁰ = 1024.

Which statement about cmd1 && cmd2 || cmd3 is true?

  1. cmd3 runs only if cmd1 fails
  2. cmd3 runs if cmd1 OR cmd2 fails
  3. cmd3 runs only if cmd2 fails
  4. 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?

  1. Speeds up pipes
  2. The exit code of a pipeline is that of the last failed command
  3. Forbids pipes
  4. 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?

  1. Deletes $tmp immediately
  2. Deletes $tmp when the script ends – even on error or Ctrl+C
  3. Prevents the script from ending
  4. Deletes $tmp only 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##*/}"
  1. bericht.tar.gz
  2. /home/ada
  3. bericht
  4. 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}"
  1. bericht.tar
  2. bericht
  3. tar.gz
  4. 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[@]}"
  1. 3
  2. 4
  3. 5
  4. 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"
  1. 3
  2. 0
  3. 1
  4. 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 ))
  1. 10
  2. 8
  3. Error
  4. 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. 1
  2. 0
  3. 127
  4. 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?

  1. Every 15 hours on every day
  2. Every 15 minutes from Monday to Friday
  3. At 3 PM on weekdays
  4. 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?

  1. The text is executed as a command
  2. Variables and $(...) in the text are not expanded
  3. The text is encrypted
  4. 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?

  1. It permanently sorts both files
  2. It compares the sorted outputs without creating intermediate files
  3. It merges both files
  4. 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?

  1. They are deprecated
  2. For licensing reasons macOS still ships Bash 3.2; they exist from Bash 4
  3. They are only allowed in zsh
  4. 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?

  1. eval "grep $eingabe datei"
  2. befehl=(grep -- "$eingabe" datei); "${befehl[@]}"
  3. bash -c "grep $eingabe datei"
  4. 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.