Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Default values for input in Bash Scripting - Interactive Code Practice

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

Complete the code to assign a default value 'guest' to the variable 'user' if no input is given.

Bash Scripting
user=${1:-[1]
echo "User is $user"
Drag options to blanks, or click blank then click option'
A$1
Bdefault
Cguest
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 directly without default value syntax.
Using = instead of :- inside the braces.
2fill in blank
medium

Complete the code to assign '8080' as the default port if no argument is provided.

Bash Scripting
port=${1:-[1]
echo "Server will run on port $port"
Drag options to blanks, or click blank then click option'
A8080
B3000
C443
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use the :- syntax for default values.
Using a port number that is not the intended default.
3fill in blank
hard

Fix the error in the code to correctly assign 'default.txt' as the filename if no argument is given.

Bash Scripting
filename=${1[1]"default.txt"}
echo "Using file: $filename"
Drag options to blanks, or click blank then click option'
A:-
B:=
C=
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':-' causes wrong behavior when variable is empty.
Omitting the colon ':' changes the meaning.
4fill in blank
hard

Fill both blanks to assign 'admin' as default user and 'root' as default group if no inputs are given.

Bash Scripting
user=${1[1]"admin"}
group=${2[2]"root"}
echo "User: $user, Group: $group"
Drag options to blanks, or click blank then click option'
A:-
B:=
C-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Mixing different default value syntaxes for user and group.
5fill in blank
hard

Fill all three blanks to create a script that assigns default values: 'localhost' for host, '3306' for port, and 'root' for user if no inputs are given.

Bash Scripting
host=${1[1]"localhost"}
port=${2[2]"3306"}
user=${3[3]"root"}
echo "Connecting to $host on port $port as $user"
Drag options to blanks, or click blank then click option'
A:-
B:=
C-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Using '-' without colon which only covers unset variables.

Practice

(1/5)
1. What does the expression ${name:-Guest} do in a bash script?
easy
A. It throws an error if name is not set.
B. It assigns 'Guest' to name permanently.
C. It deletes the variable name if it is empty.
D. It uses the value of name if set; otherwise, it uses 'Guest' as default.

Solution

  1. Step 1: Understand the syntax ${variable:-default}

    This syntax means: use the value of variable if it is set and not empty; otherwise, use default.
  2. Step 2: Apply to ${name:-Guest}

    If name is set and not empty, it returns name. If name is unset or empty, it returns 'Guest'.
  3. Final Answer:

    It uses the value of name if set; otherwise, it uses 'Guest' as default. -> Option D
  4. Quick Check:

    Default value syntax = It uses the value of name if set; otherwise, it uses 'Guest' as default. [OK]
Hint: Use ${var:-default} to provide fallback values [OK]
Common Mistakes:
  • Thinking it assigns the default permanently
  • Confusing with ${var:=default} which assigns default
  • Assuming it throws error if variable unset
2. Which of the following is the correct syntax to read a variable user with a default value 'admin' if input is empty?
easy
A. read user; user=${user:-admin}
B. read user || user=admin
C. read user && user=admin
D. read user ? user=admin

Solution

  1. Step 1: Understand how to read input and set default

    The read command takes input into user. To set a default if empty, use user=${user:-admin}.
  2. Step 2: Check each option

    read user; user=${user:-admin} correctly reads input then sets default if empty. Options A, C, D use invalid syntax for default assignment.
  3. Final Answer:

    read user; user=${user:-admin} -> Option A
  4. Quick Check:

    Correct default syntax after read = read user; user=${user:-admin} [OK]
Hint: Use read then assign default with ${var:-default} [OK]
Common Mistakes:
  • Using || or && incorrectly for default assignment
  • Trying to assign default inside read command
  • Using invalid operators like ? for default
3. What is the output of this script if the user presses Enter without typing anything?
read -p "Enter your city: " city
city=${city:-Unknown}
echo "City: $city"
medium
A. City: city
B. City:
C. City: Unknown
D. Error: city not set

Solution

  1. Step 1: Analyze read input behavior

    User presses Enter without input, so city is empty string.
  2. Step 2: Apply default value substitution

    city=${city:-Unknown} sets city to 'Unknown' because city is empty.
  3. Final Answer:

    City: Unknown -> Option C
  4. Quick Check:

    Empty input triggers default = City: Unknown [OK]
Hint: Empty input triggers default with ${var:-default} [OK]
Common Mistakes:
  • Expecting empty output instead of default
  • Confusing ${var:-default} with ${var-default}
  • Assuming error occurs on empty input
4. Identify the error in this script snippet:
read name
name=${name=John}
echo "Name: $name"
medium
A. The echo command syntax is wrong
B. The default value syntax is incorrect; should use :- instead of =
C. The variable name is reserved and cannot be used
D. The read command is missing a prompt

Solution

  1. Step 1: Check default value syntax

    The script uses ${name=John} which is invalid for default substitution. The correct syntax is ${name:-John}.
  2. Step 2: Confirm other parts

    read without prompt is valid, name is a valid variable, and echo syntax is correct.
  3. Final Answer:

    The default value syntax is incorrect; should use :- instead of = -> Option B
  4. Quick Check:

    Default syntax requires :- not = [OK]
Hint: Use ${var:-default} not ${var=default} for default values [OK]
Common Mistakes:
  • Using = instead of :- for default values
  • Thinking read needs a prompt
  • Confusing variable names with reserved words
5. You want to write a script that asks for a username and a port number. If the user presses Enter without input, the username should default to 'guest' and the port to '8080'. Which script snippet correctly implements this?
hard
A. read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port"
B. read -p "Username: " user user=${user=guest} read -p "Port: " port port=${port=8080} echo "User=$user, Port=$port"
C. read user if [ -z "$user" ]; then user=guest; fi read port if [ -z "$port" ]; then port=8080; fi echo "User=$user, Port=$port"
D. read user user=${user:-guest} read port port=${port-8080} echo "User=$user, Port=$port"

Solution

  1. Step 1: Check default value syntax for both variables

    read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" uses ${var:-default} which correctly sets default if variable is empty or unset.
  2. Step 2: Verify prompt usage and output

    read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" uses read -p to prompt user, then sets defaults, then echoes both variables correctly.
  3. Final Answer:

    read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" -> Option A
  4. Quick Check:

    Use ${var:-default} with read -p for defaults = read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" [OK]
Hint: Use read -p with ${var:-default} for clean input with defaults [OK]
Common Mistakes:
  • Using = instead of :- for defaults
  • Not prompting user with read -p
  • Using ${var-default} which skips empty but not unset
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard