














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















































1. Which special shell variable contains the exit status of the most recently executed command? A. $$ B. $# C. $? D. $! Rationale: $? stores the exit status of the last command executed. A value of 0 indicates success, while any non-zero value indicates failure. $$ is the current shell's PID, $# is the number of positional parameters, and $! stores the PID of the last background process. 2. Which command substitution syntax is recommended for modern shell scripts? A. command B. ${command} C. $(command)
D. @(command) Rationale: $(command) is preferred because it supports nesting and improves readability. Although backticks remain supported, they are more difficult to read and escape in complex scripts.
3. What is the purpose of the set - e option in a Bash script? A. Enables command history B. Ignores syntax errors C. Executes commands faster D. Terminates the script when a command exits with a non- zero status Rationale: set - e causes the shell to exit immediately when a command fails unless that failure occurs in specific contexts such as conditional tests. It is commonly used to prevent scripts from continuing after unexpected errors. 4. Which conditional expression correctly checks whether a file exists and is a regular file? A. [ - d file ] B. [ - e file ] C. [ - f file ] D. [ - r file ]
A. ${var%%pattern} B. ${var#pattern} C. ${var##pattern} D. ${var%pattern} Rationale: ${var%pattern} removes the shortest matching suffix, whereas ${var%%pattern} removes the longest suffix. Prefix removal uses # and ##.
8. Which command makes a shell script directly executable? A. chmod 600 script.sh B. chmod 444 script.sh C. chmod +x script.sh D. chmod - r script.sh Rationale: The executable permission must be set using chmod +x. The script also requires an appropriate interpreter line (shebang) if executed directly. 9. Which shebang is most portable across Linux distributions? A. #!/bin/bash B. #!/usr/bin/env bash C. #!/usr/bin/sh D. #!/usr/local/bash
Rationale: Using /usr/bin/env bash allows the system to locate Bash through the user's PATH, improving portability across systems where Bash may reside in different directories.
10. What is the effect of quoting a variable like "$file"? A. Converts text to uppercase B. Removes whitespace C. Prevents word splitting and pathname expansion D. Prevents variable expansion Rationale: Double quotes preserve spaces and special characters while still allowing variable expansion. This is considered a best practice in nearly all shell scripts. 11. Which built-in command reads user input into a variable? A. echo B. printf C. read D. cat Rationale: read accepts input from standard input and stores it in one or more variables, supporting options for prompts, delimiters, and silent input.
C. exec D. eval Rationale: exec replaces the current shell with another executable without creating a new process, making it useful in wrapper scripts and process management.
15. What is the primary purpose of the trap command? A. Encrypt variables B. Execute specified commands when signals or shell events occur C. Compress output D. Speed up script execution Rationale: trap enables cleanup routines, signal handling, and custom actions when receiving signals such as SIGINT or upon script exit. 16. Which arithmetic expansion correctly adds two variables? A. ${a+b} B. [a+b] C. $((a+b)) D. $(a+b) Rationale: $(( )) performs integer arithmetic directly within the shell without calling external utilities like expr.
17. Which command imports another shell script into the current shell environment? A. exec B. bash C. source D. fork Rationale: source (or .) executes commands from another script in the current shell, allowing variables and functions to persist after execution. 18. Which option causes Bash to report an error when an undefined variable is referenced? A. set - a B. set - o history C. set - u D. set - x Rationale: set - u treats references to unset variables as errors, helping identify programming mistakes early in script execution. 19. Which expansion generates filenames matching a wildcard pattern? A. Parameter expansion
Rationale: The regular expression ^[0-9]+$ matches strings composed entirely of one or more decimal digits, making it suitable for numeric validation.
22. Which shell option displays every command before execution for debugging? A. set - v B. set - o pipefail C. set - x D. set - b Rationale: set - x enables execution tracing by printing each expanded command before it runs, making it invaluable for debugging complex scripts. 23. What is the purpose of "$@" inside a shell script? A. Expands positional parameters into a single string. B. Counts positional parameters. C. Returns the script name. D. Expands all positional parameters while preserving individual arguments. Rationale: "$@" expands each positional parameter as a separate quoted argument, preserving spaces and special characters. This behaviour is essential when forwarding arguments safely.
24. Which command safely compares two strings in Bash? A. [ $a - eq $b ] B. [[ "$a" == "$b" ]] C. [ "$a" - gt "$b" ] D. test "$a" - ge "$b" Rationale: [[ ]] provides improved string comparison, avoids many word-splitting issues, and supports pattern matching. Numeric operators such as - eq should only be used with integers. 25. Which practice most improves the reliability and maintainability of production shell scripts? A. Avoid comments to reduce file size. B. Use unquoted variables for flexibility. C. Ignore command exit codes. D. Enable defensive scripting practices such as set - euo pipefail, quote variables consistently, validate inputs, and handle errors explicitly. Rationale: Robust shell scripting combines strict error handling, defensive coding, input validation, careful quoting, and predictable control flow. These practices minimise subtle bugs, improve portability, and significantly increase script reliability in production environments.
local total= } What is the primary advantage of declaring total as local? A. It allows the variable to be accessed by every script running on the system. B. It automatically exports the variable to child processes. C. It limits the variable's scope to the function, preventing accidental modification of global variables. D. It permanently stores the variable after the function exits. Rationale: Local variables exist only within the function in which they are declared. Once the function finishes execution, the variable disappears. Using local improves code maintainability, reduces naming conflicts, and prevents unintended side effects when functions manipulate temporary data.
28. Which command correctly appends the output of a command to an existing log file while preserving previous contents? A. command > logfile B. command 2> logfile C. command >> logfile D. command &> logfile
Rationale: The >> operator appends standard output to the end of a file without overwriting its existing contents. By contrast, > truncates the file before writing, 2> redirects only standard error, and &> redirects both standard output and standard error while overwriting unless combined with append syntax.
29. A script contains the following loop: for file in *.txt do echo "$file" done What happens if no .txt files exist and the nullglob shell option is disabled? A. The loop generates a syntax error. B. The loop is skipped entirely. C. *The loop executes once using the literal string .txt as the value of file. D. Bash automatically creates an empty text file. Rationale: By default, Bash leaves unmatched glob patterns unchanged. Therefore, if no files match *.txt, the pattern itself becomes the loop value. Enabling shopt - s nullglob changes this behaviour so unmatched patterns expand to nothing, preventing unintended processing.
stripping directory hierarchies without relying on external commands like basename.
32. What is the primary purpose of the IFS (Internal Field Separator) variable? A. It specifies the shell version. B. It defines the maximum command length. C. It determines how the shell splits input into separate fields during word splitting. D. It stores shell aliases. Rationale: IFS controls how Bash separates words during variable expansion and input processing. By default, it consists of spaces, tabs, and newlines. Modifying IFS is common when processing CSV files, colon-separated records, or custom-delimited input. 33. Examine the following code: while read line do echo "$line" done < input.txt Why is "$line" enclosed in double quotes? A. To convert the line to uppercase.
B. To preserve embedded whitespace and prevent unwanted word splitting. C. To prevent the variable from expanding. D. To remove quotation marks from the input. Rationale: Quoting variables ensures that spaces, tabs, wildcard characters, and other special characters remain intact during expansion. Omitting the quotes could cause Bash to split the line into multiple words or expand wildcard characters unexpectedly.
34. Which command displays the current shell's process ID? A. $! B. $? C. $# D. $$ Rationale: $$ expands to the process ID (PID) of the currently running shell. This value is frequently used to generate temporary filenames that are less likely to collide with those created by other processes. 35. Which command exports a variable so that child processes inherit it? A. declare B. readonly
echo "${name,,}" A. LINUX B. Linux C. linux D. xuniL Rationale: The ${variable,,} parameter expansion converts every alphabetic character to lowercase. Bash also supports ${variable^^} for uppercase conversion and ${variable^} for capitalising only the first character.
38. Which command enables strict pipeline error detection? A. set - n B. set - a C. set - o pipefail D. set - m Rationale: Normally, a pipeline returns the exit status of its final command. With set - o pipefail, the pipeline instead returns the status of the first failed command, making error detection significantly more reliable in complex scripts. 39. Why is using absolute paths within administrative scripts generally recommended? A. They consume less memory. B. They eliminate the need for executable permissions.
C. They reduce dependence on the user's PATH environment and improve script reliability. D. They automatically increase execution speed. Rationale: Administrative scripts should not assume the contents or ordering of the PATH variable. Using absolute paths ensures the intended executable is run, reducing the risk of executing an unexpected or malicious program placed earlier in the PATH.
40. Which statement about shell aliases is correct? A. Aliases are inherited by child processes automatically. B. Aliases are portable across all shells. C. Aliases are primarily intended for interactive shell use and are generally unsuitable for production scripts. D. Aliases permanently modify system commands. Rationale: Aliases simplify interactive command entry but are typically not expanded in non-interactive scripts unless explicitly enabled. Functions provide greater flexibility, portability, argument handling, and maintainability for reusable script logic. 41. Which special variable contains all command-line arguments as a single quoted string? A. $@ B. $*