Exam Contributor Shell Scripting using Bash Practice Exam, Exams of Technology

This exam evaluates the ability to design, review, and refine assessment questions for Bash scripting certification. Topics include environment variables, loops, functions, POSIX standards, debugging, file manipulation, automation workflows, and secure scripting practices. Candidates practice authoring scenario-based questions, validating correctness, reviewing distractors, applying exam blueprint methodologies, and ensuring difficulty-level alignment.

Typology: Exams

2025/2026

Available from 01/12/2026

shilpi-jain-1
shilpi-jain-1 🇮🇳

4.2

(5)

29K documents

1 / 89

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exam Contributor Shell Scripting using
Bash Practice Exam
**Question 1.** Which file is read by a login shell but not by a nonlogin interactive shell?
A) ~/.bashrc
B) /etc/profile
C) ~/.bash_logout
D) /etc/bash.bashrc
Answer: B
Explanation: /etc/profile is executed for login shells; ~/.bashrc is read by interactive nonlogin
shells.
**Question 2.** What is the primary advantage of using `#!/usr/bin/env bash` as the shebang
line?
A) Guarantees the script runs faster
B) Allows the script to locate Bash via the user's PATH
C) Enables the script to run on Windows without modification
D) Prevents the need for execute permissions
Answer: B
Explanation: `env` searches the PATH for the `bash` executable, making the script portable
across systems where Bash may reside in different locations.
**Question 3.** Which command will correctly display whether `cd` is a shell builtin?
A) type cd
B) which cd
C) help cd
D) both A and C
Answer: D
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59

Partial preview of the text

Download Exam Contributor Shell Scripting using Bash Practice Exam and more Exams Technology in PDF only on Docsity!

Bash Practice Exam

Question 1. Which file is read by a login shell but not by a non‑login interactive shell? A) ~/.bashrc B) /etc/profile C) ~/.bash_logout D) /etc/bash.bashrc Answer: B Explanation: /etc/profile is executed for login shells; ~/.bashrc is read by interactive non‑login shells. Question 2. What is the primary advantage of using #!/usr/bin/env bash as the shebang line? A) Guarantees the script runs faster B) Allows the script to locate Bash via the user's PATH C) Enables the script to run on Windows without modification D) Prevents the need for execute permissions Answer: B Explanation: env searches the PATH for the bash executable, making the script portable across systems where Bash may reside in different locations. Question 3. Which command will correctly display whether cd is a shell builtin? A) type cd B) which cd C) help cd D) both A and C Answer: D

Bash Practice Exam

Explanation: type cd shows its classification, and help cd displays builtin documentation; both confirm it is a builtin. Question 4. In Bash, which quoting mechanism prevents all variable and command expansion? A) Double quotes (" ) B) Single quotes (' ) C) Backticks (`) D) Backslashes (\) Answer: B Explanation: Single quotes preserve the literal value of each character inside them. Question 5. Which of the following statements about a non‑interactive shell is TRUE? A) It reads ~/.bashrc automatically B) It does not read /etc/profile C) It cannot execute scripts D) It does not read commands from stdin Answer: D Explanation: Non‑interactive shells read commands from scripts or other sources, not from the terminal’s stdin. Question 6. What does the command set - o vi affect? A) The default editor for crontab B) The command line editing mode C) The behavior of the cd builtin D) The handling of job control signals

Bash Practice Exam

D) set foo Answer: B Explanation: declare - p foo prints the variable’s attributes and value; echo $foo works but is not a builtin that displays the variable definition. Question 10. What is the result of the following expansion? echo ${#PATH} A) The number of directories in PATH B) The length of the string stored in PATH C) The number of characters in the first directory of PATH D) An error because ${#} is invalid syntax Answer: B Explanation: ${#var} expands to the length (in characters) of the variable’s value. Question 11. Which of the following correctly assigns the output of the command date to a variable now? A) now=date B) now=$(date) C) now=$( date ) D) both A and B Answer: D Explanation: Both backticks and the $( ) syntax capture command output; both are valid. Question 12. How can you make a variable myvar read‑only within a Bash script? A) export myvar B) readonly myvar

Bash Practice Exam

C) declare - r myvar D) both B and C Answer: D Explanation: readonly and declare - r both set the read‑only attribute. Question 13. Which of the following statements about $@ and $* is correct? A) $@ expands to a single word, $* expands to separate words B) $@ and $* are identical when quoted C) $@ preserves each positional parameter as a separate quoted word when used as "$@" D) $* is preferred for passing arguments to another script Answer: C Explanation: "$@" expands each positional parameter as a distinct quoted word, whereas "$*" concatenates them into a single string. Question 14. What does the tilde expansion ~+ represent? A) The home directory of the current user B) The previous working directory (OLDPWD) C) The current working directory (PWD) D) The root directory (/) Answer: C Explanation: ~+ expands to the current directory stored in $PWD. Question 15. Given var="Hello World", what is the result of ${var:6:5}? A) World B) Hello

Bash Practice Exam

B) 123

C) {1..3}

D) An error because brace expansion is not supported in Bash 3.x Answer: B Explanation: Brace expansion generates 1 2 3; echo - n prints them without spaces. Question 19. Which loop syntax is equivalent to while (( i < 10 )); do …; done? A) until (( i >= 10 )); do …; done B) for (( ; i < 10; )); do …; done C) while [ $i - lt 10 ]; do …; done D) Both A and C Answer: D Explanation: Both until (( i >= 10 )) and while [ $i - lt 10 ] continue while the condition is true, matching the original while. Question 20. What does the break 2 command do inside nested loops? A) Exits the innermost loop only B) Exits both the innermost and the next outer loop C) Exits all loops in the script D) Causes a syntax error Answer: B Explanation: break n terminates n levels of enclosing loops; break 2 exits two levels. Question 21. Which redirection operator appends both standard output and standard error to the same file?

Bash Practice Exam

A) >> file 2>&1 B) > file 2>> file C) &>> file (Bash‑specific) D) Both A and C Answer: D Explanation: >> file 2>&1 appends stdout then redirects stderr to the same descriptor; &>> file is a Bash shortcut that does the same. Question 22. How can you redirect only the standard error of a command to error.log while discarding its standard output? A) command > /dev/null 2> error.log B) command 2> error.log > /dev/null C) command >& error.log D) Both A and B Answer: D Explanation: Both orderings correctly send stdout to /dev/null and stderr to error.log. Question 23. Which of the following pipelines will correctly count the number of lines that contain the word “error” in syslog? A) grep - c error syslog B) grep error syslog | wc - l C) cat syslog | grep - c error D) All of the above Answer: D Explanation: All three produce the same line count; grep - c directly counts matches.

Bash Practice Exam

B) cat <<'EOF' newline line1 newline line2 newline line3 newline EOF C) cat <<< $'line1\nline2\nline3' D) Both B and C Answer: D Explanation: Both a quoted here‑document (B) and a here‑string with $'...' (C) correctly supply the three lines. Question 27. Which syntax defines a Bash function named process that accepts arguments? A) function process { … } B) process() { … } C) function process() { … } D) All of the above Answer: D Explanation: All three are valid ways to define a function; parentheses are optional after function. Question 28. What is the exit status of a Bash function that ends with return 5? A) 5 B) 0 C) The exit status of the last command executed inside the function D) 255 (since Bash limits to 0‑255) Answer: A Explanation: return sets the function’s exit status to the given integer (modulo 256).

Bash Practice Exam

Question 29. How can you source a script lib.sh located in the same directory as the current script, regardless of the current working directory? A) . ./lib.sh B) source $PWD/lib.sh C) source "$(dirname "$0")/lib.sh" D) source lib.sh Answer: C Explanation: $(dirname "$0") resolves the directory of the running script, making the path independent of the caller’s cwd. Question 30. Which of the following glob patterns matches any file that starts with “data” and ends with a three‑digit number? A) data[0-9][0-9][0-9] B) data??? C) data[[:digit:]]{3} (requires extglob) D) Both A and C when shopt - s extglob is enabled Answer: D Explanation: Pattern A works without extglob; pattern C needs extglob ({3} quantifier). Both match the described files. Question 31. In Bash regular expression matching, which operator is used? A) = B) == C) =~ D) =: Answer: C

Bash Practice Exam

Answer: A Explanation: -i ignores case when matching patterns. Question 35. How does the tr command differ from sed? A) tr works on a per‑character basis, while sed works on lines/patterns B) tr can perform regular expression substitutions C) tr can insert text, while sed cannot D) tr reads from standard input only, sed cannot Answer: A Explanation: tr translates or deletes characters; sed applies scriptable text transformations using patterns. Question 36. Which command will sort the file data.txt numerically and remove duplicate lines? A) sort - u - n data.txt B) sort - n data.txt | uniq C) sort - n data.txt - u D) Both A and C Answer: D Explanation: Both -u (unique) and -n (numeric) options can be combined; order of options does not matter. Question 37. What does the exit status $? represent after a pipeline like cmd1 | cmd2 finishes? A) The exit status of cmd1 B) The exit status of cmd2

Bash Practice Exam

C) The sum of both exit statuses D) Always 0 if the pipeline succeeds Answer: B Explanation: By default, $? holds the exit status of the last command in the pipeline (cmd2). Question 38. Which Bash option causes the script to terminate when an undefined variable is referenced? A) set - e B) set - u C) set - o pipefail D) set - x Answer: B Explanation: -u (nounset) treats the use of undefined variables as an error. Question 39. In Bash, what does the construct cmd && echo "OK" do? A) Executes echo only if cmd fails B) Executes echo only if cmd succeeds (exit status 0) C) Executes echo regardless of cmd’s result D) Executes cmd twice Answer: B Explanation: && performs short‑circuit evaluation; the right side runs only when the left side succeeds. Question 40. Which set option enables tracing of each command after it is expanded but before execution? A) -e

Bash Practice Exam

B) Sends the command’s output to a file named “&” C) Executes the command with elevated privileges D) Waits for the command to finish before proceeding Answer: A Explanation: Appending & starts the command as a background job. Question 44. Which of the following commands will list all currently running background jobs of the shell? A) jobs - l B) ps - ef C) bg D) fg Answer: A Explanation: jobs reports the status of jobs started from the current shell; -l includes their PIDs. Question 45. How can you schedule a script backup.sh to run every day at 2 am using cron? A) Add 0 2 * * * /path/backup.sh to the user’s crontab B) Run at 02:00 backup.sh C) Use systemctl timer backup.timer D) Place the script in /etc/cron.daily Answer: A Explanation: The cron time specification 0 2 * * * runs the command at 02:00 daily.

Bash Practice Exam

Question 46. Which command changes the ownership of file.txt to user alice and group staff? A) chmod alice:staff file.txt B) chown alice:staff file.txt C) chgrp staff file.txt && chown alice file.txt D) Both B and C Answer: D Explanation: chown directly sets both owner and group; alternatively, two separate commands achieve the same result. Question 47. What is the effect of the command umask 027 before creating new files? A) New files will have permissions 644 B) New files will have permissions 600 C) New files will have permissions 750 D) New files will have permissions 755 Answer: B Explanation: umask subtracts bits from the default 666 (files) and 777 (dirs). 666‑027 = 640, but for regular files the execute bit is never set, resulting in 640 (rw‑r‑—). However typical answer expects 640; but most exams accept 640. Correction: The correct resulting permission is 640 (rw‑r‑—). Since option B (600) is closest, but to avoid confusion we’ll adjust: Revised Answer: New files will have permissions 640. Explanation: umask 027 removes group write and all others permissions, leaving rw‑r‑— (640) for files.

Bash Practice Exam

Question 51. In a Bash script, how can you prevent a variable from being exported to child processes? A) Use local var=value inside a function B) Use unset - f var C) Use declare - x var D) Variables are always exported by default Answer: A Explanation: local limits the variable’s scope to the function; it is not exported. Question 52. What does the ${parameter:-word} expansion do? A) Assigns word to parameter if parameter is unset or null B) Expands to parameter if set; otherwise expands to word without assigning C) Throws an error if parameter is unset D) Replaces parameter with word permanently Answer: B Explanation: The :- operator provides a default value for expansion only; it does not assign. Question 53. Which test operator checks whether a variable var is an integer using Bash regex? A) [[ $var =~ ^[0-9]+$ ]] B) [ - int $var ] C) test $var - eq $var D) [[ $var - eq $var ]] Answer: A

Bash Practice Exam

Explanation: The =~ operator with a regex pattern validates the string format. Question 54. How can you display the first 10 lines of a file log.txt without using head? A) sed - n '1,10p' log.txt B) awk 'NR<=10' log.txt C) cat log.txt | while read; do echo $REPLY; done | break 10 (invalid) D) Both A and B Answer: D Explanation: Both sed and awk can print a range of lines. Question 55. Which command will replace the third occurrence of “foo” with “bar” on each line of file.txt? A) sed 's/foo/bar/3' file.txt B) awk '{sub(/foo/,"bar",3)}1' file.txt C) perl - pe 's/foo/bar/3' file.txt D) All of the above Answer: D Explanation: All three utilities support specifying the occurrence number. Question 56. In Bash, what does the [[ - z $var ]] test evaluate? A) True if $var is an empty string B) True if $var is non‑empty C) True if $var is undefined D) True if $var contains only whitespace Answer: A