Unit 3: Bash Scripting Fundamentals - Practice Quiz

CSC104 — It Fundamentals 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is a Bash script?

Introduction to Bash scripting Easy
A. A database used to store commands
B. A configuration file that only controls the graphical desktop environment
C. A text file containing Bash commands
D. A compiled file containing machine code

2 Which line is commonly used as the shebang at the beginning of a Bash script?

Bash script structure Easy
A. #!/bin/bash
B. #!/bin/java
C. #!/usr/bin/compiler
D. #!/usr/bin/python

3 Which Bash command displays Hello World on the terminal?

Hello World Easy
A. print(Hello World)
B. echo "Hello World"
C. show Hello World
D. display = Hello World

4 Which statement correctly assigns the value Alice to a Bash variable named name?

Variables Easy
A. set name Alice
B. $name=Alice
C. name = Alice
D. name="Alice"

5 Which Bash command reads keyboard input into a variable named username?

User input Easy
A. read username
B. input username
C. username=$(collect all available keyboard input from the current terminal)
D. scan username

6 Which symbol begins a single-line comment in a Bash script?

Comments Easy
A. //
B. /*
C. #
D. --

7 Which special parameter represents the first argument passed to a Bash script?

Arguments Easy
A. $0
B. $1
C. $#
D. $@

8 Which expression accesses the first element of a Bash array named colors?

Arrays Easy
A. ${colors[1]}
B. ${colors[first_element_of_the_array]}
C. ${colors[0]}
D. $colors(0)

9 If word="BashScript", what does ${word:0:4} produce?

String slicing Easy
A. ashS
B. Bash
C. Script
D. BashScript

10 Which Bash test checks whether a file stored in $file exists?

Conditional expressions (file, string, arithmetic) Easy
A. [ -z "$file" ]
B. [ -d "$file" ]
C. [ "$file" -eq "an existing file on the current file system" ]
D. [ -e "$file" ]

11 What does an exit status of 0 normally mean in Bash?

Exit status Easy
A. The command was not found
B. The command must be run again with administrator permissions
C. The command received no arguments
D. The command completed successfully

12 Which keyword closes an if statement in Bash?

If-else statements Easy
A. end
B. fi
C. esac
D. done

13 Which keyword closes a Bash case statement?

Switch-case statements Easy
A. done
B. fi
C. esac
D. endcase

14 Which loop repeats commands for each item in a list?

Loops (for, while, until) Easy
A. A case statement
B. A for loop
C. A while loop that always requires a numeric counter and a separate update command
D. An if statement

15 What does break do when it is executed inside a Bash loop?

Break and continue statements Easy
A. It restarts the entire script
B. It exits the current loop
C. It pauses the loop until the user enters another command
D. It skips only the current iteration

16 How is a Bash function named greet commonly called after it has been defined?

Functions Easy
A. execute(greet)
B. run function greet
C. greet
D. call greet()

17 Which command enables execution tracing in a Bash script?

Debugging Easy
A. set -x
B. set -u
C. set --trace every command together with a complete explanation of its behavior
D. set -e

18 Which keyboard shortcut commonly stops the command currently running in a terminal?

Shortcuts Easy
A. Ctrl+E
B. Ctrl+L
C. Ctrl+A
D. Ctrl+C

19 What is generally required before a Bash script can be run directly as ./myscript.sh?

Custom commands Easy
A. It must use a .command extension
B. It must have execute permission
C. It must be stored in the root directory
D. It must be converted into a compiled binary containing native machine instructions

20 Where can a user commonly place an alias so that it is available in future Bash sessions?

Persistent changes Easy
A. In /bin/bash
B. In ~/.bashrc
C. In the command history for only the currently open terminal session
D. In /tmp/bash_aliases

21 A script named report.sh has execute permission and begins with #!/usr/bin/env bash. What happens when the user runs ./report.sh?

Introduction to Bash scripting Medium
A. The operating system locates bash through PATH and uses it to run the script
B. The operating system compiles the script into a native executable before running it
C. The script runs only when /usr/bin/bash exists as a regular executable file
D. The current interactive shell interprets the script without starting another process

22 Which sequence gives a sensible structure for a Bash script that must stop when a required command fails?

Bash script structure Medium
A. Shell options, shebang, main commands, constants, functions
B. Functions, main commands, constants, shell options, shebang
C. Constants, main commands, shebang, functions, shell options
D. Shebang, shell options, constants, functions, main commands

23 Which command reliably prints Hello World followed by a newline without depending on shell-specific handling of escape sequences?

Hello World Medium
A. echo '%s\n' 'Hello World'
B. printf 'Hello' ' World\n'
C. printf '%s\n' 'Hello World'
D. echo -e '%s\n' 'Hello World'

24 Assume file="annual report.txt". Which command passes the entire filename to wc as one argument?

Variables Medium
A. wc -l '$file'
B. wc -l $file
C. wc -l ${file[*]}
D. wc -l "$file"

25 A script must read a full name, including spaces, into the variable name without displaying the input. Which command is appropriate?

User input Medium
A. read -d ' ' -p "Name: " name
B. read -a -p "Name: " name
C. read -r -s -p "Name: " name
D. read -r -n 1 -p "Name: " name

26 Why does # not begin a comment in the command echo "Status: #ready"?

Comments Medium
A. The character is escaped automatically whenever it appears after a command
B. The character begins a comment only when it is the first character of a file
C. The character follows a colon and is interpreted as part of a label
D. The character is inside double quotes and is treated as literal text

27 A script must forward all of its positional arguments to another command while preserving each original argument boundary. Which expansion should it use?

Arguments Medium
A. $#
B. $@
C. "$@"
D. "$*"

28 Given files=("first draft.txt" "final.txt" "notes.md"), which loop processes each filename as one complete value?

Arrays Medium
A. for file in "${files[*]}"; do process "$file"; done
B. for file in "${files[@]}"; do process "$file"; done
C. for file in ${files[@]}; do process "$file"; done
D. for file in "${#files[@]}"; do process "$file"; done

29 Given code="BASH2025", what does ${code:4:2} produce?

String slicing Medium
A. 2025
B. H2
C. 20
D. SH

30 Which condition succeeds only when path refers to an existing regular file and count is greater than or equal to ?

Conditional expressions (file, string, arithmetic) Medium
A. [[ -d $path && $count -ge 10 ]]
B. [[ -f $path && $count -ge 10 ]]
C. [[ -f $path || $count -gt 10 ]]
D. [[ -e $path && $count > 10 ]]

31 What is printed by grep -q "root" /etc/passwd; status=$?; echo "$status" when grep finds a matching line?

Exit status Medium
A. 2
B. root
C. 1
D. 0

32 A script should print available only if service --check succeeds; otherwise, it should print unavailable. Which construct does this correctly?

If-else statements Medium
A. if $(service --check); then echo available; else echo unavailable; fi
B. if service --check; then echo available; else echo unavailable; fi
C. if service --check; do echo available; else echo unavailable; done
D. if [ service --check ]; then echo available; else echo unavailable; fi

33 Which case pattern matches filenames ending in either .jpg or .png?

Switch-case statements Medium
A. *.jpg,*.png)
B. *.{jpg,png})
C. *.jpg|*.png)
D. [*.jpg|*.png])

34 Assume attempt=0. Which loop runs a command repeatedly until it succeeds, while incrementing attempt after each failure?

Loops (for, while, until) Medium
A. until ! command; do ((attempt++)); done
B. for command; do ((attempt++)); done
C. while command; do ((attempt++)); done
D. until command; do ((attempt++)); done

35 Inside a loop over filenames, the script should skip directories but stop entirely when it encounters STOP. Which statements implement that behavior?

Break and continue statements Medium
A. [[ -d $file ]] && return; [[ $file == STOP ]] && exit
B. [[ -d $file ]] && continue; [[ $file == STOP ]] && break
C. [[ -d $file ]] && break; [[ $file == STOP ]] && continue
D. [[ -d $file ]] && exit; [[ $file == STOP ]] && continue

36 A function must return a calculated text value such as report.txt to its caller. Which approach is appropriate?

Functions Medium
A. Export a local variable and read it from the parent shell
B. Pass the value to return and read it from standard output
C. Print the value with printf and capture it using command substitution
D. Assign the value to $? and access it after the function call

37 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?

Debugging Medium
A. set -x and set +x
B. set -e and set +e
C. set -u and set +u
D. set -n and set +n

38 In an interactive Bash session, which shortcut searches backward through command history as the user types part of an earlier command?

Shortcuts Medium
A. Ctrl-R
B. Ctrl-A
C. Ctrl-U
D. Ctrl-L

39 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?

Custom commands Medium
A. Rename the script to backup.sh and remove execute permission
B. Move the script into the current directory and run source backup
C. Add $HOME/bin to PATH and reload the shell configuration
D. Add $HOME/bin/backup to PATH and restart the script

40 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?

Persistent changes Medium
A. exec ~/.bashrc
B. export ~/.bashrc
C. source ~/.bashrc
D. bash ~/.bashrc

41 A script named setup.sh contains cd /tmp and export MODE=test. Which invocation makes both changes affect the current interactive Bash session?

Introduction to Bash scripting Hard
A. (source ./setup.sh)
B. bash ./setup.sh
C. ./setup.sh
D. . ./setup.sh

42 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?

Bash script structure Hard
A. Move the shebang to byte zero and retain execute permission
B. Move the shebang first and remove execute permission
C. Keep the blank line and add another shebang below it
D. Keep the layout and add # bash script at the end

43 A variable may contain percent signs, backslashes, spaces, or -n. Which command reliably prints its value followed by exactly one newline?

Hello World Hard
A. printf '%s\n' "$message"
B. echo -e "$message"
C. echo $message
D. printf "$message\n"

44 What does payload=$(printf 'A\n\n'); printf '<%s>\n' "$payload" print?

Variables Hard
A. <A> followed by three newlines
B. <A> followed by one newline
C. <A> followed by two newlines
D. <A\n\n> followed by one newline

45 A user enters two leading spaces, a\b, and two trailing spaces. Which command stores all those characters unchanged in value?

User input Hard
A. IFS= read value
B. IFS= read -r value
C. read -r value
D. read value

46 Consider : <<'BLOCK', followed by lines containing $HOME and $(touch marker), and then a line containing only BLOCK. What happens when Bash executes this construct?

Comments Hard
A. Only $HOME expands, and marker is not created
B. Both expansions run, and marker is created
C. The text causes a syntax error before execution
D. Neither expansion runs, and marker is not created

47 After set -- "a b" "" c, what is printed by for x in "$@"; do printf '[%s]' "$x"; done?

Arguments Hard
A. [a][b][c]
B. [a b][c]
C. [a][b][][c]
D. [a b][][c]

48 Given a=([2]=x [5]=y); unset 'a[2]'; a+=(z), what does printf '%s|%s\n' "${!a[@]}" "${#a[@]}" print?

Arrays Hard
A. 5 6|2
B. 5 6|7
C. 5|2
D. 0 1|2

49 With s=abcdef, which expansion produces de?

String slicing Hard
A. ${s:3:-2}
B. ${s: -3:2}
C. ${s: -2:3}
D. ${s:-3:2}

50 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?

Conditional expressions (file, string, arithmetic) Hard
A. [[ -L $p && $s == a* ]] && (( 10#$n == 8 ))
B. [[ -L $p && $s == "a*" ]] && (( 10#$n == 8 ))
C. [[ -L $p && $s == a* ]] && (( $n == 8 ))
D. [[ -e $p && $s == a* ]] && (( 10#$n == 8 ))

51 What is printed by set -o pipefail; false | (exit 3) | true; printf '%s\n' "$?"?

Exit status Hard
A. 141
B. 1
C. 3
D. 0

52 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?

If-else statements Hard
A. Both branches run because assignment succeeds
B. The else branch prints absent
C. Neither branch runs because result is empty
D. The then branch prints found

53 What is printed by case foobar in foo*) printf generic ;; f??bar) printf specific ;; *bar) printf suffix ;; esac?

Switch-case statements Hard
A. generic
B. specific
C. suffix
D. genericspecificsuffix

54 What is printed by i=0; while (( i < 4 )); do ((i++)); (( i == 2 )) && continue; printf '%s' "$i"; done?

Loops (for, while, until) Hard
A. 1234
B. 134
C. 0134
D. 0123

55 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?

Break and continue statements Hard
A. 1xend
B. 1x2xend
C. 1x1yend
D. 1xinnerend

56 What is printed by x=global; g() { printf '%s\n' "$x"; }; f() { local x=outer; g; }; f in Bash?

Functions Hard
A. global
B. An unbound-variable error
C. An empty line
D. outer

57 Which setup enables execution tracing and defers expansion of source file, line number, and function name until each trace line is emitted?

Debugging Hard
A. BASH_XTRACEFD=2; PS4='trace: '; set -v
B. PS4="+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: "; set -x
C. PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '; set -v
D. PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '; set -x

58 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?

Shortcuts Hard
A. Ctrl-A Ctrl-E
B. Alt-. Ctrl-E
C. Ctrl-X Ctrl-E
D. Ctrl-R Ctrl-E

59 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?

Custom commands Hard
A. chmod u-x ~/.local/bin/report; export PATH="$PATH:$HOME"; hash report
B. chmod u+r ~/.local/bin/report; export PATH="$PATH/report"; hash -t report
C. chmod u+x ~/.local/bin/report; export HOME="$HOME/.local/bin"; hash -d
D. chmod u+x ~/.local/bin/report; export PATH="$HOME/.local/bin:$PATH"; hash -r

60 Which arrangement persistently defines alias ll='ls -alF' for both interactive login shells and interactive non-login Bash shells?

Persistent changes Hard
A. Put the alias only in ~/.bash_profile and delete ~/.bashrc
B. Put the alias in ~/.bashrc and source that file from ~/.bash_profile
C. Put the alias in a script under ~/bin and mark it executable
D. Put the alias only in ~/.profile and unset BASH_ENV