Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which loop repeats commands for each item in a list?
Not quite. Try another option.
Answer: A for loop
A for loop can iterate through each item in a list and run commands for every item.
Question 15 of 60 · Break and continue statements · Easy
What does break do when it is executed inside a Bash loop?
Not quite. Try another option.
Answer: It exits the current loop
The break statement immediately stops the current loop and continues execution after it.
Question 16 of 60 · Functions · Easy
How is a Bash function named greet commonly called after it has been defined?
Not quite. Try another option.
Answer: greet
A Bash function is called by writing its name as a command.
Question 17 of 60 · Debugging · Easy
Which command enables execution tracing in a Bash script?
Not quite. Try another option.
Answer: set -x
set -x prints commands and their expanded arguments as Bash executes them.
Question 18 of 60 · Shortcuts · Easy
Which keyboard shortcut commonly stops the command currently running in a terminal?
Not quite. Try another option.
Answer: Ctrl+C
Ctrl+C sends an interrupt signal that normally stops the foreground command.
Question 19 of 60 · Custom commands · Easy
What is generally required before a Bash script can be run directly as ./myscript.sh?
Not quite. Try another option.
Answer: It must have execute permission
The script needs execute permission, commonly added with chmod +x myscript.sh, before direct execution.
Question 20 of 60 · Persistent changes · Easy
Where can a user commonly place an alias so that it is available in future Bash sessions?
Not quite. Try another option.
Answer: In ~/.bashrc
Commands and aliases placed in ~/.bashrc are loaded for future interactive Bash sessions.
Question 21 of 60 · Introduction to Bash scripting · Medium
A script named report.sh has execute permission and begins with #!/usr/bin/env bash. What happens when the user runs ./report.sh?
Not quite. Try another option.
Answer: The operating system locates bash through PATH and uses it to run the script
The shebang uses env to find bash through the current PATH, which is more portable than specifying a fixed Bash location.
Question 22 of 60 · Bash script structure · Medium
Which sequence gives a sensible structure for a Bash script that must stop when a required command fails?
Not quite. Try another option.
Answer: Shebang, shell options, constants, functions, main commands
The shebang should be first. Options such as set -e are normally enabled early, followed by definitions and the main execution logic.
Question 23 of 60 · Hello World · Medium
Which command reliably prints Hello World followed by a newline without depending on shell-specific handling of escape sequences?
Not quite. Try another option.
Answer: printf '%s\n' 'Hello World'
printf applies the %s placeholder to Hello World and then prints \n. Its behavior is more predictable than echo options.
Question 24 of 60 · Variables · Medium
Assume file="annual report.txt". Which command passes the entire filename to wc as one argument?
Not quite. Try another option.
Answer: wc -l "$file"
Double-quoting "$file" preserves the embedded space while still expanding the variable.
Question 25 of 60 · User input · Medium
A script must read a full name, including spaces, into the variable name without displaying the input. Which command is appropriate?
Not quite. Try another option.
Answer: read -r -s -p "Name: " name
-r prevents backslash processing, -s hides typed input, and -p displays the prompt. The entire line is stored in name.
Question 26 of 60 · Comments · Medium
Why does # not begin a comment in the command echo "Status: #ready"?
Not quite. Try another option.
Answer: The character is inside double quotes and is treated as literal text
A # inside quotes is ordinary text. An unquoted # can begin a comment when it occurs where a new shell word could start.
Question 27 of 60 · Arguments · Medium
A script must forward all of its positional arguments to another command while preserving each original argument boundary. Which expansion should it use?
Not quite. Try another option.
Answer: "$@"
"$@" expands each positional parameter as a separate quoted argument, preserving spaces and empty arguments.
Question 28 of 60 · Arrays · Medium
Given files=("first draft.txt" "final.txt" "notes.md"), which loop processes each filename as one complete value?
Not quite. Try another option.
Answer: for file in "${files[@]}"; do process "$file"; done
Quoted "${files[@]}" expands each array element separately, so filenames containing spaces remain intact.
Question 29 of 60 · String slicing · Medium
Given code="BASH2025", what does ${code:4:2} produce?
Not quite. Try another option.
Answer: 20
Bash substring indexes start at zero. Offset 4 starts at the first 2, and length 2 selects 20.
Question 30 of 60 · Conditional expressions (file, string, arithmetic) · Medium
Which condition succeeds only when path refers to an existing regular file and count is greater than or equal to ?
Not quite. Try another option.
Answer: [[ -f $path && $count -ge 10 ]]
-f tests for a regular file, && requires both conditions, and -ge performs an arithmetic greater-than-or-equal comparison.
Question 31 of 60 · Exit status · Medium
What is printed by grep -q "root" /etc/passwd; status=$?; echo "$status" when grep finds a matching line?
Not quite. Try another option.
Answer: 0
A command normally returns exit status 0 on success. The value is saved immediately because $? changes after another command runs.
Question 32 of 60 · If-else statements · Medium
A script should print available only if service --check succeeds; otherwise, it should print unavailable. Which construct does this correctly?
Not quite. Try another option.
Answer: if service --check; then echo available; else echo unavailable; fi
An if statement directly evaluates the command's exit status. A zero status selects the then branch.
Question 33 of 60 · Switch-case statements · Medium
Which case pattern matches filenames ending in either .jpg or .png?
Not quite. Try another option.
Answer: *.jpg|*.png)
Within a Bash case statement, alternative patterns are separated with | before the closing parenthesis.
Question 34 of 60 · Loops (for, while, until) · Medium
Assume attempt=0. Which loop runs a command repeatedly until it succeeds, while incrementing attempt after each failure?
Not quite. Try another option.
Answer: until command; do ((attempt++)); done
An until loop continues while its condition command fails and stops when the command returns a successful exit status.
Question 35 of 60 · Break and continue statements · Medium
Inside a loop over filenames, the script should skip directories but stop entirely when it encounters STOP. Which statements implement that behavior?
continue skips the rest of the current iteration, while break exits the nearest enclosing loop.
Question 36 of 60 · Functions · Medium
A function must return a calculated text value such as report.txt to its caller. Which approach is appropriate?
Not quite. Try another option.
Answer: Print the value with printf and capture it using command substitution
Bash return supports only an integer exit status. Text results are commonly printed and captured with syntax such as result=$(function_name).
Question 37 of 60 · Debugging · Medium
A script should display expanded commands only around a suspicious section and then return to normal execution. Which pair of commands should surround that section?
Not quite. Try another option.
Answer: set -x and set +x
set -x enables execution tracing, and set +x disables it after the relevant section.
Question 38 of 60 · Shortcuts · Medium
In an interactive Bash session, which shortcut searches backward through command history as the user types part of an earlier command?
Not quite. Try another option.
Answer: Ctrl-R
Ctrl-R starts a reverse incremental history search. Typing more characters narrows the matching previous commands.
Question 39 of 60 · Custom commands · Medium
A user creates an executable script named backup in $HOME/bin, but typing backup reports command not found. The script works with $HOME/bin/backup. What is the most likely fix?
Not quite. Try another option.
Answer: Add $HOME/bin to PATH and reload the shell configuration
Bash searches directories listed in PATH. The directory containing the executable must be included, not the full path to the script.
Question 40 of 60 · Persistent changes · Medium
A user adds export EDITOR=vim to ~/.bashrc. How can the user apply the change to the current Bash session without opening a new terminal?
Not quite. Try another option.
Answer: source ~/.bashrc
source executes the configuration file in the current shell, allowing its variable and alias changes to persist in that session.
Question 41 of 60 · Introduction to Bash scripting · Hard
A script named setup.sh contains cd /tmp and export MODE=test. Which invocation makes both changes affect the current interactive Bash session?
Not quite. Try another option.
Answer: . ./setup.sh
The . command sources the script in the current shell. The other forms execute it in a separate shell or subshell, so their environment changes do not persist.
Question 42 of 60 · Bash script structure · Hard
An executable script has a blank line before #!/usr/bin/env bash. Which change ensures the operating system recognizes and uses the intended interpreter when the script is run as ./tool?
Not quite. Try another option.
Answer: Move the shebang to byte zero and retain execute permission
A kernel-recognized shebang must begin with #! at the start of the file. Direct execution also requires execute permission.
Question 43 of 60 · Hello World · Hard
A variable may contain percent signs, backslashes, spaces, or -n. Which command reliably prints its value followed by exactly one newline?
Not quite. Try another option.
Answer: printf '%s\n' "$message"
A constant format string prevents data containing % from being interpreted as formatting directives, while quoting preserves the value exactly.
Question 44 of 60 · Variables · Hard
What does payload=$(printf 'A\n\n'); printf '<%s>\n' "$payload" print?
Not quite. Try another option.
Answer: <A> followed by one newline
Command substitution removes all trailing newline characters from its output. The final printf then contributes one newline.
Question 45 of 60 · User input · Hard
A user enters two leading spaces, a\b, and two trailing spaces. Which command stores all those characters unchanged in value?
Not quite. Try another option.
Answer: IFS= read -r value
An empty IFS preserves leading and trailing whitespace, and -r prevents the backslash from acting as an escape character.
Question 46 of 60 · Comments · Hard
Consider : <<'BLOCK', followed by lines containing $HOME and $(touch marker), and then a line containing only BLOCK. What happens when Bash executes this construct?
Not quite. Try another option.
Answer: Neither expansion runs, and marker is not created
Quoting the here-document delimiter disables parameter and command substitution. The resulting text is supplied to :, which ignores its input.
Question 47 of 60 · Arguments · Hard
After set -- "a b" "" c, what is printed by for x in "$@"; do printf '[%s]' "$x"; done?
Not quite. Try another option.
Answer: [a b][][c]
Quoted "$@" expands each positional argument as a separate word, preserving both embedded spaces and empty arguments.
Question 48 of 60 · Arrays · Hard
Given a=([2]=x [5]=y); unset 'a[2]'; a+=(z), what does printf '%s|%s\n' "${!a[@]}" "${#a[@]}" print?
Not quite. Try another option.
Answer: 5 6|2
After index 2 is removed, appending uses index 6, one greater than the highest existing index. The array therefore has indices 5 and 6 and contains two elements.
Question 49 of 60 · String slicing · Hard
With s=abcdef, which expansion produces de?
Not quite. Try another option.
Answer: ${s: -3:2}
The space distinguishes a negative substring offset from the :- default-value operator. Offset -3 begins at d, and length 2 selects de.
Question 50 of 60 · Conditional expressions (file, string, arithmetic) · Hard
p names a symbolic link whose target does not exist, s=abc, and n=08. Which condition succeeds without interpreting 08 as an invalid octal number?
-L detects the dangling link, the unquoted right side of == acts as a pattern, and 10# explicitly treats the number as base 10.
Question 51 of 60 · Exit status · Hard
What is printed by set -o pipefail; false | (exit 3) | true; printf '%s\n' "$?"?
Not quite. Try another option.
Answer: 3
With pipefail, a pipeline returns the status of its rightmost command that exited nonzero. Here that command is the subshell exiting with status 3.
Question 52 of 60 · If-else statements · Hard
An existing file contains no line beginning with key=. Which branch runs in if result=$(grep -m1 '^key=' file); then echo found; else echo absent; fi?
Not quite. Try another option.
Answer: The else branch prints absent
An assignment containing command substitution receives the substituted command's exit status. grep returns 1 when no line matches, so the condition is false.
Question 53 of 60 · Switch-case statements · Hard
What is printed by case foobar in foo*) printf generic ;; f??bar) printf specific ;; *bar) printf suffix ;; esac?
Not quite. Try another option.
Answer: generic
Bash selects the first matching case pattern. It does not choose the most specific pattern, and ;; prevents fallthrough.
Question 54 of 60 · Loops (for, while, until) · Hard
What is printed by i=0; while (( i < 4 )); do ((i++)); (( i == 2 )) && continue; printf '%s' "$i"; done?
Not quite. Try another option.
Answer: 134
The increment occurs before printing. The iteration where i becomes 2 continues immediately, so only 1, 3, and 4 are printed.
Question 55 of 60 · Break and continue statements · Hard
What is printed by for a in 1 2; do for b in x y z; do [[ $b == y ]] && break 2; printf '%s%s' "$a" "$b"; done; printf inner; done; printf end?
Not quite. Try another option.
Answer: 1xend
break 2 exits both enclosing loops when b is y. The first iteration prints 1x, and execution then continues with the final printf end.
Question 56 of 60 · Functions · Hard
What is printed by x=global; g() { printf '%s\n' "$x"; }; f() { local x=outer; g; }; f in Bash?
Not quite. Try another option.
Answer: outer
Bash local variables are dynamically scoped. While f is active, a function called by f can see f's local value of x.
Question 57 of 60 · Debugging · Hard
Which setup enables execution tracing and defers expansion of source file, line number, and function name until each trace line is emitted?
Not quite. Try another option.
Answer: PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '; set -x
Single quotes prevent expansion during assignment, allowing PS4 fields to expand for each traced command. set -x enables execution tracing; set -v only prints input lines.
Question 58 of 60 · Shortcuts · Hard
With Bash Readline using the Emacs keymap, which shortcut opens the current command line in the editor configured by $VISUAL or $EDITOR, then executes the edited command when the editor closes?
Not quite. Try another option.
Answer: Ctrl-X Ctrl-E
Ctrl-X Ctrl-E invokes Readline's edit-and-execute-command, which transfers the current command to the configured editor.
Question 59 of 60 · Custom commands · Hard
A script has been installed as ~/.local/bin/report during the current Bash session. Which command sequence makes it directly executable by name and refreshes Bash's command-location cache?
Nearly through. Did unit 3 of CSC104 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of CSC104 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.