[{"id":"bashintermediate-001","courseId":"bash-intermediate","domain":"syntax-and-commands","topic":"Pipeline status with pipefail","prompt":"A script turns on pipefail, then runs the pipeline below and prints $?. What does it print?","hint":"Ask which stage's status the pipeline reports when pipefail is on.","explain":"A pipeline's exit status is normally taken from one of its commands. The pipefail option changes which command's status is used, so a failure in the middle of a pipe can show up.","source":"https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html","interaction":{"type":"single"},"options":[{"id":"a","text":"0","whyNot":"The last command, true, succeeded, but pipefail makes a failing earlier stage count too."},{"id":"b","text":"3","why":"With pipefail, the status is that of the rightmost command that exits non-zero: the first subshell, which exits 3."},{"id":"c","text":"1","whyNot":"Bash doesn't turn a failed pipeline into 1; it passes on the real status of the failing command."},{"id":"d","text":"4","whyNot":"4 is the number of commands in the pipeline; pipefail never reports a count."}],"answer":"b","explanation":"With pipefail set, a pipeline returns the status of the rightmost command that exits non-zero, or zero if every command succeeds.","concept":"With pipefail set, a pipeline returns the status of the rightmost command that exits non-zero, or zero if every command succeeds.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"set -o pipefail\n(exit 3) | (exit 0) | true\necho $?","takeaways":["With pipefail on, a failing stage in the middle of a pipe still makes the pipeline's status non-zero."]},{"id":"bashintermediate-002","courseId":"bash-intermediate","domain":"syntax-and-commands","topic":"Exceptions to set -e","prompt":"With set -e turned on, a command that fails as the test of an if statement does not make the shell exit.","hint":"Ask whether the shell is using that status to pick a branch or to report a failure.","explain":"set -e makes the shell exit on a non-zero status. It has exceptions in certain positions inside compound commands and lists. Consider what those positions use the status for.","source":"https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"The shell does not exit when the failing command is part of an if test; that is one of errexit's exceptions."},{"id":"b","text":"False","whyNot":"The if test is listed as an exception, so a failure there does not stop the shell."}],"answer":"a","explanation":"errexit ignores a failing command that is part of an if test or a while or until test, and all but the last command of an && or || list.","concept":"errexit ignores a failing command that is part of an if test or a while or until test, and all but the last command of an && or || list.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["set -e skips failures in if and while/until tests, and in all but the last command of && or || lists."]},{"id":"bashintermediate-003","courseId":"bash-intermediate","domain":"syntax-and-commands","topic":"Chaining && and ||","prompt":"A script runs the line below. grep finds no match, so it exits non-zero. What happens to the echo?","hint":"Ask what the left command must return before the right one is reached.","explain":"Every command ends with an exit status: zero for success, anything else for failure. List operators read that number to decide what runs next. Think about which value the right-hand command needs.","source":"https://www.gnu.org/software/bash/manual/html_node/Lists.html","interaction":{"type":"single"},"options":[{"id":"a","text":"echo is skipped, because the right side of && runs only after a zero exit status","why":"&& runs its right side only when the left command succeeds, so a failed grep means echo never runs."},{"id":"b","text":"echo runs, because && runs both sides and only reports the failure","whyNot":"&& is conditional; it doesn't run the right side after a failure."},{"id":"c","text":"echo runs, because grep printed nothing, which counts as success","whyNot":"The exit status decides, not whether output appears; a failed grep returns non-zero."},{"id":"d","text":"echo runs, because && only waits for the left command to finish","whyNot":"Waiting isn't the rule; && checks the exit status of the left command."}],"answer":"a","explanation":"In command1 && command2, command2 runs only if command1 exits with status zero; in command1 || command2, command2 runs only on a non-zero status.","concept":"In command1 && command2, command2 runs only if command1 exits with status zero; in command1 || command2, command2 runs only on a non-zero status.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"grep -q needle haystack.txt && echo found","takeaways":["When a command joined by && fails, the command after the && is skipped."]},{"id":"bashintermediate-004","courseId":"bash-intermediate","domain":"syntax-and-commands","topic":"Subshells lose variable changes","prompt":"The script below changes count inside parentheses, then inside braces, and prints it after each. Which TWO statements about the output are correct?","hint":"Ask whether the group creates a separate environment or runs in this one.","explain":"Commands can be grouped with parentheses or braces, and the two forms run in different places. Ask whether a change made inside a group still exists once the group has finished.","source":"https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"The first echo prints 0, since the change inside parentheses was made in a subshell.","why":"Parentheses run their list in a subshell, and its variable assignments are lost when it ends."},{"id":"b","text":"The second echo prints 5, since braces run the list in the current shell.","why":"Braces run the list in the current shell environment with no subshell, so the assignment stays."},{"id":"c","text":"The first echo prints 5, since parentheses only group commands and create no environment.","whyNot":"Parentheses do create a subshell, so the assignment is discarded."},{"id":"d","text":"The second echo prints 0, since braces also run their list in a subshell.","whyNot":"Braces create no subshell; the semicolon before the closing brace is required syntax."},{"id":"e","text":"Both echos print 5, since grouped commands share variables with the whole script.","whyNot":"The parenthesized change is lost, so the first echo still prints 0."},{"id":"f","text":"Both echos print 0, since a top-level assignment can't be changed by a later command.","whyNot":"A plain assignment in the current shell does change the variable, so the braces version prints 5."}],"answer":"ab","explanation":"Parentheses run a list in a subshell, so its variable assignments are lost afterwards; braces run the list in the current shell, so assignments persist.","concept":"Parentheses run a list in a subshell, so its variable assignments are lost afterwards; braces run the list in the current shell, so assignments persist.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"count=0\n( count=5 )\necho \"$count\"\ncount=0\n{ count=5; }\necho \"$count\"","takeaways":["Parentheses run their commands in a subshell, so variable changes made inside them don't survive."]},{"id":"bashintermediate-005","courseId":"bash-intermediate","domain":"syntax-and-commands","topic":"Arithmetic for loops","prompt":"A script counts down with the loop below and echoes i in the body. Which values does it print?","hint":"Trace the variable by hand: set it, test it, run the body, then step it.","explain":"A C-style for loop has three arithmetic parts: a setup, a test, and a step. Work through them by hand, checking the test before each pass and the step after it.","source":"https://www.gnu.org/software/bash/manual/html_node/Looping-Constructs.html","interaction":{"type":"single"},"options":[{"id":"a","text":"3, 2, 1","why":"The test is checked before each pass, the body runs while i is above zero, and the step runs after each pass."},{"id":"b","text":"3, 2, 1, 0","whyNot":"Once i reaches 0 the test is false, so the loop stops before printing it."},{"id":"c","text":"0, 1, 2","whyNot":"The loop starts at 3 and counts down, so it never counts up from zero."},{"id":"d","text":"2, 1","whyNot":"The step runs after each pass, so the first value, 3, is printed before any decrement."}],"answer":"a","explanation":"In for (( expr1; expr2; expr3 )), expr1 runs once, expr2 is tested before each pass, and expr3 runs after each pass the test allowed.","concept":"In for (( expr1; expr2; expr3 )), expr1 runs once, expr2 is tested before each pass, and expr3 runs after each pass the test allowed.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"for (( i = 3; i > 0; i-- )); do\n  echo $i\ndone","takeaways":["In a C-style for loop, the body runs only while the test is non-zero."]}]