702-100 practice question 6 of 15

Which of the following excerpts is a valid if condition in the Bourne shell?

Full set: 60 questions
Answer options
A: exam question Correct answer
B: exam question
C: exam question
D: exam question
Correct Answer:
A. https://kxbjsyuhceggsyvxdkof.supabase.co/storage/v1/object/public/file-images/_LPI_702-100/page_3_img_1.jpg
Explanation

The Bourne shell if statement executes a command and checks its exit status. The [ is not part of the if syntax itself; it is a command, also known as test. The test command evaluates the expression that follows it and exits with a status of 0 (true) or 1 (false). In the original Bourne shell and the POSIX standard for the test utility, the single equals sign (=) is the correct operator for string comparison. Therefore, if [ $a = $b ] is the valid and portable syntax for checking if the strings stored in variables $a and $b are identical.

Why Incorrect

B: The double equals sign (==) is a non-standard extension for string comparison within [ ... ]. While supported by Bash and other modern shells, it is not valid in the original Bourne shell or strictly POSIX-compliant shells. C: Parentheses ( ... ) are used to execute commands in a subshell. This syntax is not used for conditional expressions in shell scripting and would result in a command-not-found error if $a is not an executable. D: The double-bracket [[ ... ]] is a compound command and a keyword, not a standard utility. It is an extended feature found in shells like Bash and ksh but is not part of the original Bourne shell.

References

1. The Open Group Base Specifications Issue 7, 2018 edition (POSIX.1-2017), test utility: In the "OPERANDS" section, the specification for string comparison is explicitly defined as s1 = s2. The == operator is not mentioned, confirming it is not part of the standard. (See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html)

2. GNU Bash Reference Manual, Section 6.4 Bash Conditional Expressions: The manual states, "When used with the test or [ builtins, == is a synonym for =." This documents == as a feature of Bash's test builtin, but also confirms that = is the fundamental operator. The manual also describes [[...]] as a separate, more advanced conditional construct. (See https://www.gnu.org/software/bash/manual/htmlnode/Bash-Conditional-Expressions.html)

3. Massachusetts Institute of Technology (MIT) OpenCourseWare, 6.828: Operating System Engineering, Lecture 5: Shell: The lecture notes on shell scripting demonstrate the use of the test command (or [) with the single equals sign (=) for string comparison, reinforcing this as the standard and taught practice for portable shell scripts. (See course materials related to shell scripting fundamentals).

Scroll to Top