0
0
Bash Scriptingscripting~20 mins

Default values for input in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script using default values?
Consider the following Bash script snippet. What will it print when run without any arguments?
Bash Scripting
name=${1:-"Guest"}
echo "Hello, $name!"
AHello, Guest!
BHello, $1!
CHello, !
DSyntax error
Attempts:
2 left
💡 Hint
Look at how the variable 'name' is assigned using ${1:-"Guest"}.
💻 Command Output
intermediate
2:00remaining
What does this script output when argument is provided?
What will this Bash script print if run with the argument "Alice"?
Bash Scripting
user=${1:-"Unknown"}
echo "User: $user"
AUser: Alice
BUser: $1
CUser: Unknown
DUser:
Attempts:
2 left
💡 Hint
Check how the default value is used only if the argument is missing.
📝 Syntax
advanced
2:00remaining
Which option correctly assigns a default value to a variable if input is empty?
You want to assign the variable 'color' to the first argument or default to 'blue' if the argument is empty or unset. Which option is correct?
Acolor=$1 || blue
Bcolor=${1-blue}
Ccolor=${1:=blue}
Dcolor=${1:-blue}
Attempts:
2 left
💡 Hint
Remember that :- uses default if variable is unset or empty.
🔧 Debug
advanced
2:00remaining
Why does this script print an empty string instead of the default?
Given this script: ```bash input=$1 value=${input-"default"} echo "$value" ``` When run without arguments, it prints an empty line. Why?
ABecause echo is missing quotes
BBecause the quotes around default cause a syntax error
CBecause input is set but empty, so ${input-"default"} does not use default
DBecause input is not exported
Attempts:
2 left
💡 Hint
Check what happens when assigning input=$1 with $1 unset, and note the - operator (no colon).
🚀 Application
expert
2:00remaining
How many items are in the array after this script runs?
Consider this Bash script: ```bash args=(${@:-"default"}) echo ${#args[@]} ``` What number will it print if run without any arguments?
A0
B1
CThe number of positional parameters
DSyntax error
Attempts:
2 left
💡 Hint
Think about how ${@:-"default"} behaves when no arguments are passed; ${@} expands to null.