0
0
Bash Scriptingscripting~10 mins

String comparisons (=, !=, -z, -n) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable name is equal to "Alice".

Bash Scripting
if [ "$name" [1] "Alice" ]; then
  echo "Hello, Alice!"
fi
Drag options to blanks, or click blank then click option'
A=
B-n
C-z
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=' when checking for equality.
Using '-z' or '-n' which check string length, not equality.
2fill in blank
medium

Complete the code to check if the variable input is not empty.

Bash Scripting
if [ [1] "$input" ]; then
  echo "Input received."
fi
Drag options to blanks, or click blank then click option'
A!=
B-z
C-n
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-z' which checks if the string is empty.
Using '=' or '!=' which compare strings but don't check emptiness.
3fill in blank
hard

Fix the error in the code to check if var is empty.

Bash Scripting
if [ [1] "$var" ]; then
  echo "Variable is empty."
fi
Drag options to blanks, or click blank then click option'
A!=
B=
C-n
D-z
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or '!=' without a second string to compare.
Using '-n' which checks for non-empty strings.
4fill in blank
hard

Fill both blanks to check if user is not "admin" and is not empty.

Bash Scripting
if [ "$user" [1] "admin" ] && [ [2] "$user" ]; then
  echo "Access denied."
fi
Drag options to blanks, or click blank then click option'
A!=
B-z
C-n
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '!=' for inequality.
Using '-z' instead of '-n' to check non-empty string.
5fill in blank
hard

Fill all three blanks to create a condition that checks if file is empty or equals "default.txt".

Bash Scripting
if [ [1] "$file" ] || [ "$file" [2] [3] ]; then
  echo "Using default file."
fi
Drag options to blanks, or click blank then click option'
A-z
B=
C"default.txt"
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=' for equality check.
Not quoting the string "default.txt".