LFCS Certification Shell Scripting and Automation 2 — Questions and Answers
Question 1: What does the command substitution syntax $(command) do in a bash script?
- Runs the command in the background as a subshell
- Captures the stdout output of a command and inserts it inline (Correct answer)
- Defines a new subshell environment variable
- Redirects command output to /dev/null silently
Correct answer: Captures the stdout output of a command and inserts it inline
Command substitution executes the command inside $() and replaces the expression with the command's standard output.
Question 2: In a bash script, how do you correctly access the third positional argument passed to the script?
- $0
- $2
- $3 (Correct answer)
- $arg3
Correct answer: $3
$3 refers to the third positional parameter; $0 is the script name, $1 is the first argument, and so on.
Question 3: What does the `shift` command do when called in a bash script?
- Shifts all positional parameters left by one, dropping $1 (Correct answer)
- Moves script execution to another function definition
- Changes the current working directory to the parent
- Reverses the order of all positional parameters
Correct answer: Shifts all positional parameters left by one, dropping $1
shift discards $1 and renumbers the remaining positional parameters so that $2 becomes $1, $3 becomes $2, and so on.
Question 4: Which test expression correctly checks that a path exists and is a regular file (not a directory)?
- [ -d filename ]
- [ -e filename ]
- [ -f filename ] (Correct answer)
- [ -r filename ]
Correct answer: [ -f filename ]
-f returns true only if the path exists and is a regular file, while -e returns true for any existing path regardless of type.
Question 5: What is the key distinction between the `=` operator and `-eq` in bash conditional tests?
- `=` is for arithmetic comparisons and `-eq` is for string comparisons
- `=` compares strings lexicographically and `-eq` compares integers numerically (Correct answer)
- They are fully interchangeable inside single brackets
- `-eq` requires double brackets [[ ]] while `=` works in both
Correct answer: `=` compares strings lexicographically and `-eq` compares integers numerically
Using = for integer comparison may work by coincidence but performs string comparison, while -eq correctly evaluates numeric equality.
Question 6: Which syntax correctly defines a bash function named `backup`?
- backup() { commands; } (Correct answer)
- def backup() { commands; }
- backup: function { commands; }
- sub backup { commands; }
Correct answer: backup() { commands; }
Bash functions are defined with the syntax name() { body; } or using the optional 'function' keyword before the name.
Question 7: What is the effect of adding `set -e` at the top of a bash script?
- Enables extended glob patterns for filename matching
- Causes the script to exit immediately when any command returns a non-zero status (Correct answer)
- Sources environment variables from an external file
- Enables debug mode, printing each command before it runs
Correct answer: Causes the script to exit immediately when any command returns a non-zero status
set -e (errexit) makes the script abort on the first command failure, preventing silent errors from cascading.
What does the command substitution syntax $(command) do in a bash script?