Unit 3: Bash Scripting Fundamentals - Practice Quiz
1 What is a Bash script?
2 Which line is commonly used as the shebang at the beginning of a Bash script?
#!/bin/bash
#!/bin/java
#!/usr/bin/compiler
#!/usr/bin/python
3
Which Bash command displays Hello World on the terminal?
print(Hello World)
echo "Hello World"
show Hello World
display = Hello World
4
Which statement correctly assigns the value Alice to a Bash variable named name?
set name Alice
$name=Alice
name = Alice
name="Alice"
5
Which Bash command reads keyboard input into a variable named username?
read username
input username
username=$(collect all available keyboard input from the current terminal)
scan username
6 Which symbol begins a single-line comment in a Bash script?
//
/*
#
--
7 Which special parameter represents the first argument passed to a Bash script?
$0
$1
$#
$@
8
Which expression accesses the first element of a Bash array named colors?
${colors[1]}
${colors[first_element_of_the_array]}
${colors[0]}
$colors(0)
9
If word="BashScript", what does ${word:0:4} produce?
ashS
Bash
Script
BashScript
10
Which Bash test checks whether a file stored in $file exists?
[ -z "$file" ]
[ -d "$file" ]
[ "$file" -eq "an existing file on the current file system" ]
[ -e "$file" ]
11
What does an exit status of 0 normally mean in Bash?
12
Which keyword closes an if statement in Bash?
end
fi
esac
done
13
Which keyword closes a Bash case statement?
done
fi
esac
endcase
14 Which loop repeats commands for each item in a list?
case statement
for loop
while loop that always requires a numeric counter and a separate update command
if statement
15
What does break do when it is executed inside a Bash loop?
16
How is a Bash function named greet commonly called after it has been defined?
execute(greet)
run function greet
greet
call greet()
17 Which command enables execution tracing in a Bash script?
set -x
set -u
set --trace every command together with a complete explanation of its behavior
set -e
18 Which keyboard shortcut commonly stops the command currently running in a terminal?
Ctrl+E
Ctrl+L
Ctrl+A
Ctrl+C
19
What is generally required before a Bash script can be run directly as ./myscript.sh?
.command extension
20 Where can a user commonly place an alias so that it is available in future Bash sessions?
/bin/bash
~/.bashrc
/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?
bash through PATH and uses it to run the script
/usr/bin/bash exists as a regular executable file
22 Which sequence gives a sensible structure for a Bash script that must stop when a required command fails?
23
Which command reliably prints Hello World followed by a newline without depending on shell-specific handling of escape sequences?
echo '%s\n' 'Hello World'
printf 'Hello' ' World\n'
printf '%s\n' 'Hello World'
echo -e '%s\n' 'Hello World'
24
Assume file="annual report.txt". Which command passes the entire filename to wc as one argument?
wc -l '$file'
wc -l $file
wc -l ${file[*]}
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?
read -d ' ' -p "Name: " name
read -a -p "Name: " name
read -r -s -p "Name: " name
read -r -n 1 -p "Name: " name
26
Why does # not begin a comment in the command echo "Status: #ready"?
27 A script must forward all of its positional arguments to another command while preserving each original argument boundary. Which expansion should it use?
$#
$@
"$@"
"$*"
28
Given files=("first draft.txt" "final.txt" "notes.md"), which loop processes each filename as one complete value?
for file in "${files[*]}"; do process "$file"; done
for file in "${files[@]}"; do process "$file"; done
for file in ${files[@]}; do process "$file"; done
for file in "${#files[@]}"; do process "$file"; done
29
Given code="BASH2025", what does ${code:4:2} produce?
2025
H2
20
SH
30
Which condition succeeds only when path refers to an existing regular file and count is greater than or equal to ?
[[ -d $path && $count -ge 10 ]]
[[ -f $path && $count -ge 10 ]]
[[ -f $path || $count -gt 10 ]]
[[ -e $path && $count > 10 ]]
31
What is printed by grep -q "root" /etc/passwd; status=$?; echo "$status" when grep finds a matching line?
2
root
1
0
32
A script should print available only if service --check succeeds; otherwise, it should print unavailable. Which construct does this correctly?
if $(service --check); then echo available; else echo unavailable; fi
if service --check; then echo available; else echo unavailable; fi
if service --check; do echo available; else echo unavailable; done
if [ service --check ]; then echo available; else echo unavailable; fi
33
Which case pattern matches filenames ending in either .jpg or .png?
*.jpg,*.png)
*.{jpg,png})
*.jpg|*.png)
[*.jpg|*.png])
34
Assume attempt=0. Which loop runs a command repeatedly until it succeeds, while incrementing attempt after each failure?
until ! command; do ((attempt++)); done
for command; do ((attempt++)); done
while command; do ((attempt++)); done
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?
[[ -d $file ]] && return; [[ $file == STOP ]] && exit
[[ -d $file ]] && continue; [[ $file == STOP ]] && break
[[ -d $file ]] && break; [[ $file == STOP ]] && continue
[[ -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?
return and read it from standard output
printf and capture it using command substitution
$? 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?
set -x and set +x
set -e and set +e
set -u and set +u
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?
Ctrl-R
Ctrl-A
Ctrl-U
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?
backup.sh and remove execute permission
source backup
$HOME/bin to PATH and reload the shell configuration
$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?
exec ~/.bashrc
export ~/.bashrc
source ~/.bashrc
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?
(source ./setup.sh)
bash ./setup.sh
./setup.sh
. ./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 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?
printf '%s\n' "$message"
echo -e "$message"
echo $message
printf "$message\n"
44
What does payload=$(printf 'A\n\n'); printf '<%s>\n' "$payload" print?
<A> followed by three newlines
<A> followed by one newline
<A> followed by two newlines
<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?
IFS= read value
IFS= read -r value
read -r value
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?
$HOME expands, and marker is not created
marker is created
marker is not created
47
After set -- "a b" "" c, what is printed by for x in "$@"; do printf '[%s]' "$x"; done?
[a][b][c]
[a b][c]
[a][b][][c]
[a b][][c]
48
Given a=([2]=x [5]=y); unset 'a[2]'; a+=(z), what does printf '%s|%s\n' "${!a[@]}" "${#a[@]}" print?
5 6|2
5 6|7
5|2
0 1|2
49
With s=abcdef, which expansion produces de?
${s:3:-2}
${s: -3:2}
${s: -2:3}
${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?
[[ -L $p && $s == a* ]] && (( 10#$n == 8 ))
[[ -L $p && $s == "a*" ]] && (( 10#$n == 8 ))
[[ -L $p && $s == a* ]] && (( $n == 8 ))
[[ -e $p && $s == a* ]] && (( 10#$n == 8 ))
51
What is printed by set -o pipefail; false | (exit 3) | true; printf '%s\n' "$?"?
141
1
3
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?
else branch prints absent
result is empty
then branch prints found
53
What is printed by case foobar in foo*) printf generic ;; f??bar) printf specific ;; *bar) printf suffix ;; esac?
generic
specific
suffix
genericspecificsuffix
54
What is printed by i=0; while (( i < 4 )); do ((i++)); (( i == 2 )) && continue; printf '%s' "$i"; done?
1234
134
0134
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?
1xend
1x2xend
1x1yend
1xinnerend
56
What is printed by x=global; g() { printf '%s\n' "$x"; }; f() { local x=outer; g; }; f in Bash?
global
outer
57 Which setup enables execution tracing and defers expansion of source file, line number, and function name until each trace line is emitted?
BASH_XTRACEFD=2; PS4='trace: '; set -v
PS4="+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: "; set -x
PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '; set -v
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?
Ctrl-A Ctrl-E
Alt-. Ctrl-E
Ctrl-X Ctrl-E
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?
chmod u-x ~/.local/bin/report; export PATH="$PATH:$HOME"; hash report
chmod u+r ~/.local/bin/report; export PATH="$PATH/report"; hash -t report
chmod u+x ~/.local/bin/report; export HOME="$HOME/.local/bin"; hash -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?
~/.bash_profile and delete ~/.bashrc
~/.bashrc and source that file from ~/.bash_profile
~/bin and mark it executable
~/.profile and unset BASH_ENV
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →