702-100 practice question 6 of 15
Which of the following excerpts is a valid if condition in the Bourne shell?
Full set: 60 questionsThe 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.
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.
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).
Keep practicing
Take the free 702-100 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 702-100 practice questions
- Question 1Which command can be used to display the local filesystems that are currently mounted'?
- Question 2Which line in a cron job runs myscript once per hour?
- Question 3Which FreeBSD command updates packages to newer versions'?
- Question 4Assuming a umask of 022, what are the default permissions in octal mode of the newly crea…
- Question 5A file called mybackup is found while investigating a USB pen drive. Which utility can be…
- Question 7Which of the following lines in /etc/fstab mounts the device /dev/cd0a to /cdrom?
