Bash Scripting - ConditionalsWhich of the following is the correct syntax to test if a string variable str is not empty?A[ "$str" = "" ]B[ -n "$str" ]C[ -z "$str" ]D[ $str ]Check Answer
Step-by-Step SolutionSolution:Step 1: Understand -n and -z tests-n tests if string length is not zero (not empty), -z tests if string length is zero (empty).Step 2: Identify correct test for not empty[ -n "$str" ] uses -n which correctly tests if str is not empty.Final Answer:[ -n "$str" ] -> Option BQuick Check:Non-empty string test = -n [OK]Quick Trick: Use -n to check non-empty strings safely [OK]Common Mistakes:MISTAKESUsing -z for not emptyNot quoting variable causing errorsUsing [ $str ] which can fail if empty
Master "Conditionals" in Bash Scripting9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Bash Scripting Quizzes Bash Scripting Basics - Shebang line (#!/bin/bash) - Quiz 10hard Conditionals - if-elif-else - Quiz 2easy Loops - while loop - Quiz 12easy Quoting and Expansion - Double quotes (variable expansion) - Quiz 1easy Quoting and Expansion - Single quotes (literal strings) - Quiz 10hard User Input - Shifting arguments (shift) - Quiz 2easy User Input - Why input makes scripts interactive - Quiz 1easy User Input - Shifting arguments (shift) - Quiz 4medium User Input - read command - Quiz 9hard Variables - Accessing variables ($var and ${var}) - Quiz 11easy