

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This exam validates intermediate-to-advanced skill in writing Bash scripts for automation, system management, and workflow optimization. It covers shell variables, arrays, loops, conditionals, functions, process control, regex usage, file manipulation, error handling, and security considerations. Hands-on tasks require debugging scripts, building CLI utilities, automating backups, parsing logs, and interacting with system processes.
Typology: Exams
1 / 89
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which shebang line guarantees that the script will always be interpreted by Bash, even on systems where /bin/sh points to a different shell? A) #!/bin/sh B) #!/usr/bin/env sh C) #!/bin/bash D) #!/usr/bin/env bash Answer: C Explanation: #!/bin/bash explicitly invokes the Bash binary, ensuring the script runs under Bash regardless of the system’s default /bin/sh. Question 2. To make a script executable for the owner, group, and others, which chmod command should be used? A) chmod 700 script.sh B) chmod 755 script.sh C) chmod 644 script.sh D) chmod +x script.sh Answer: B Explanation: chmod 755 sets read, write, and execute for the owner and read/execute for group and others, which is the typical permission for executable scripts. Question 3. Which command marks a variable CONFIG as read‑only, preventing any later reassignment? A) export CONFIG B) declare - r CONFIG C) readonly CONFIG D) set - r CONFIG
Answer: C Explanation: readonly (or declare - r) makes a variable immutable; readonly CONFIG is the direct syntax. Question 4. In Bash, which of the following correctly references the name of the script being executed? A) $0 B) $1 C) $# D) $@ Answer: A Explanation: $0 expands to the command name used to invoke the script. Question 5. What is the effect of the command export PATH="/usr/local/bin:$PATH" inside a script? A) Makes PATH available to child processes of the script. B) Sets PATH as a local variable in the script. C) Deletes the original PATH. D) Marks PATH as read‑only. Answer: A Explanation: export marks a variable for inheritance by child processes, allowing them to see the modified PATH. Question 6. Which file is executed once for a login shell but not for non‑login interactive shells? A) ~/.bashrc
Question 9. Which command correctly declares an associative array called colors? A) declare - A colors B) declare - a colors C) colors=([red]=#FF0000) D) set - A colors Answer: A Explanation: declare - A creates an associative array; option C assigns a value but does not declare the array type. Question 10. What does the test expression [[ - f "$file" && - r "$file" ]] evaluate? A) Whether $file exists and is a regular file. B) Whether $file exists, is a regular file, and is readable. C) Whether $file is a directory and readable. D) Whether $file is a symbolic link. Answer: B Explanation: -f checks for a regular file, -r checks readability; both conditions must be true. Question 11. In a Bash if statement, which keyword must terminate the block before the else clause? A) fi B) done C) esac D) then Answer: D Explanation: The syntax is if condition; then ... else ... fi. then starts the block, and fi ends it after any else.
Question 12. Which of the following case patterns will match any string that starts with log_ and ends with .txt? A) log_*.txt) B) log_*.txt*) C) *log_*.txt) D) log_.*txt) Answer: A Explanation: In a case statement, * matches any characters; log_*.txt) matches strings starting with log_ and ending with .txt. Question 13. How many times will the loop for i in {1..5}; do echo $i; done output a number? A) 4 B) 5 C) 6 D) It depends on the Bash version. Answer: B Explanation: Brace expansion {1..5} produces the sequence 1 2 3 4 5, so the loop runs five times. Question 14. Which loop will continue iterating until the variable count reaches 10? A) while [ $count - lt 10 ]; do ... done B) until [ $count - ge 10 ]; do ... done C) for (( count=0; count<10; count++ )); do ... done D) repeat 10; do ... done
C) date 2> log.txt D) date >& log.txt Answer: A Explanation: > redirects stdout and overwrites the target file. Question 18. How would you redirect both stdout and stderr of make to a single file build.log? A) make > build.log 2>&1 B) make 2> build.log &> build.log C) make &> build.log D) All of the above Answer: D Explanation: All listed forms correctly merge stdout and stderr into build.log; &> is Bash shorthand. Question 19. What does the pipeline ps aux | grep bash accomplish? A) Lists all processes named exactly “bash”. B) Shows the process tree for bash. C) Filters the output of ps aux to lines containing the word “bash”. D) Replaces “bash” with “grep”. Answer: C Explanation: The pipe passes ps aux output to grep, which selects lines containing “bash”. Question 20. Which read option makes the input invisible (useful for passwords)? A) -t
B) -p C) -s D) -n Answer: C Explanation: -s (silent) disables echoing of typed characters. Question 21. How can you prompt the user with “Enter name:” and store the input in variable name? A) read - p "Enter name:" name B) read name "Enter name:" C) read - i "Enter name:" name D) read - e name "Enter name:" Answer: A Explanation: -p displays a prompt before reading input. Question 22. Which syntax correctly creates a here‑document that feeds the lines “Hello” and “World” to cat? A) cat <<EOF\nHello\nWorld\nEOF B) cat <<-EOF\nHello\nWorld\nEOF C) cat <<<"Hello\nWorld" D) cat <<END\nHello\nWorld\nEND Answer: A Explanation: <<EOF starts a here‑document; the delimiter EOF must appear alone on the closing line. Question 23. What is the result of tr 'a-z' 'A-Z' <<< "bash"?
A) sort list.txt | uniq B) uniq list.txt C) awk '!seen[$0]++' list.txt D) cat list.txt | uniq Answer: C Explanation: uniq only works on consecutive duplicate lines; the awk one removes duplicates while keeping order. Question 27. What does the regular expression ^[0-9]{3}-[A-Z]{2}$ match? A) Three digits, a hyphen, then two uppercase letters, and nothing else. B) Any string containing three digits followed by two letters. C) A phone number format. D) A date in YYYY‑MM format. Answer: A Explanation: ^ anchors start, $ anchors end; {3} and {2} enforce exact counts. Question 28. Which sed command deletes all blank lines from input.txt? A) sed '/^$/d' input.txt B) sed 's/^$//' input.txt C) sed - n '/./p' input.txt D) Both A and C Answer: D Explanation: /^$/d deletes empty lines; -n '/./p' prints only non‑empty lines—both achieve the goal.
Question 29. How would you replace every occurrence of “apple” with “orange” in a file fruits.txt in‑place? A) sed 's/apple/orange/g' fruits.txt B) sed - i 's/apple/orange/g' fruits.txt C) awk '{gsub(/apple/,\"orange\")}1' fruits.txt D) perl - pe 's/apple/orange/g' fruits.txt Answer: B Explanation: -i tells sed to edit the file directly. Question 30. Which arithmetic expansion yields the remainder when 17 is divided by 5? A) $((17 % 5)) B) $((17 / 5)) C) $((17 %% 5)) D) $((17 mod 5)) Answer: A Explanation: % is the modulo operator in Bash arithmetic expansion. Question 31. To perform floating‑point division 7 / 3 and obtain a decimal result, which tool should you use? A) $((7/3)) B) expr 7 / 3 C) bc <<< "scale=2; 7/3" D) let "7/3" Answer: C Explanation: Bash only supports integer arithmetic; bc provides arbitrary‑precision floating‑point calculations.
Explanation: greet() { … } is the standard Bash function syntax. Question 35. Inside a function, which keyword ensures that a variable does not affect the global environment? A) global B) export C) local D) declare Answer: C Explanation: local var=value confines the variable to the function’s scope. Question 36. What is the meaning of a function’s return status in Bash? A) The numeric value printed to stdout. B) An integer between 0‑255 indicating success (0) or error (non‑zero). C) The number of arguments passed to the function. D) The length of the function’s name. Answer: B Explanation: return sets the function’s exit status; by convention, 0 means success. Question 37. Which command imports the contents of library.sh into the current script, allowing its functions to be used? A) source library.sh B) . library.sh C) include library.sh D) Both A and B
Answer: D Explanation: Both source and the dot (.) built‑in perform the same operation. Question 38. After running a command, how can you test whether it succeeded? A) if [ $? - eq 0 ]; then … B) if $?; then … C) if [[ $? == success ]]; then … D) if $?.status; then … Answer: A Explanation: $? holds the exit status; 0 indicates success. Question 39. Which trap line will execute the function cleanup when the script receives SIGINT (Ctrl‑C)? A) trap cleanup SIGINT B) trap "cleanup" INT C) trap cleanup EXIT D) Both A and B Answer: D Explanation: SIGINT and INT are synonymous; both forms correctly set the trap. Question 40. What does set - e do in a Bash script? A) Prints each command before executing it. B) Exits the script immediately if any command returns a non‑zero status. C) Enables extended globbing. D) Treats unset variables as an error.
Answer: A Explanation: Appending & launches the preceding command as a background job. Question 44. After starting a background job, which built‑in displays a list of current jobs with their statuses? A) ps B) jobs C) bg D) fg Answer: B Explanation: jobs reports jobs managed by the current shell. Question 45. Which command brings the most recently stopped job to the foreground? A) bg % B) fg % C) jobs - r D) wait % Answer: B Explanation: fg resumes a stopped job in the foreground; % refers to the most recent job. Question 46. How can you wait for all background jobs to finish before the script proceeds? A) wait B) wait - n C) jobs - w
D) sleep 0 Answer: A Explanation: wait without arguments blocks until all child jobs have terminated. Question 47. Which command lists all processes whose command name contains “apache” and displays only their PIDs? A) ps - ef | grep apache | awk '{print $2}' B) pgrep apache C) pidof apache D) Both B and C (assuming the system supports them) Answer: D Explanation: Both pgrep and pidof return PIDs of matching processes. Question 48. What does the command kill - 9 1234 do? A) Sends SIGTERM to process 1234. B) Sends SIGKILL (signal 9) to terminate process 1234 immediately. C) Gracefully stops process 1234. D) Restarts process 1234. Answer: B Explanation: Signal 9 (SIGKILL) forces immediate termination and cannot be trapped. Question 49. In Bash, what is the effect of placing a command inside parentheses, e.g., (cd /tmp; ls)? A) Executes the commands in the current shell. B) Executes the commands in a subshell, leaving the current directory unchanged.
C) -s 5 D) -l 5 Answer: A Explanation: -n specifies the maximum number of characters to read. Question 53. How can you append the output of echo "log" to a file out.txt without creating the file if it does not exist? A) echo "log" >> out.txt B) echo "log" >| out.txt C) echo "log" > out.txt D) echo "log" 2>> out.txt Answer: A Explanation: >> appends; if the file does not exist, it is created. The requirement “without creating” cannot be satisfied; the closest answer is A but it does create if missing. Since the question expects knowledge that redirection creates the file, the correct answer is A, acknowledging creation. Question 54. Which sed flag enables in‑place editing while creating a backup file with extension .bak? A) -i.bak B) -i --backup=.bak C) -b .bak D) -e .bak Answer: A Explanation: -i.bak tells sed to edit files in place and keep a backup with the given suffix.
Question 55. What does the cut - d':' - f1 /etc/passwd command display? A) The usernames (first field) from the password file. B) The user IDs (second field). C) The full lines containing a colon. D) The number of fields per line. Answer: A Explanation: -d':' sets colon as delimiter; -f1 selects the first field, which is the username. Question 56. Which sort option sorts the input numerically rather than alphabetically? A) -n B) -r C) -k D) -u Answer: A Explanation: -n tells sort to compare according to numeric value. Question 57. How would you count the number of lines, words, and characters in a file data.txt? A) wc data.txt B) count data.txt C) awk '{print NR, NF}' data.txt D) grep - c '' data.txt Answer: A Explanation: wc (word count) reports lines, words, and bytes/characters.