Challenge - 5 Problems
Command Substitution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of command substitution with backticks
What is the output of this Bash script snippet?
Bash Scripting
current_date=`date +%Y-%m-%d` echo "Today is $current_date"
Attempts:
2 left
💡 Hint
Backticks run the command inside and replace it with the output.
✗ Incorrect
The backticks execute the date command and assign its output to the variable. The echo then prints the string with the date.
💻 Command Output
intermediate2:00remaining
Output of command substitution with $()
What will this Bash script print?
Bash Scripting
files_count=$(ls | wc -l)
echo "Number of files: $files_count"Attempts:
2 left
💡 Hint
The $() runs the command inside and replaces it with the output.
✗ Incorrect
The command inside $() runs first, counting files, then the result is stored in files_count and printed.
📝 Syntax
advanced2:00remaining
Identify the syntax error in command substitution
Which option contains a syntax error in command substitution?
Attempts:
2 left
💡 Hint
Command substitution requires backticks or $() around the command.
✗ Incorrect
Option A misses the parentheses or backticks around the command, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this command substitution fail?
Given the script:
result=`grep 'pattern file.txt`
echo "$result"
Why does this script fail?
Bash Scripting
result=`grep 'pattern file.txt` echo "$result"
Attempts:
2 left
💡 Hint
Check the quotes inside the backticks carefully.
✗ Incorrect
The single quote after pattern is not closed, causing a syntax error inside the command substitution.
🚀 Application
expert3:00remaining
Combine command substitution with arithmetic expansion
What is the output of this Bash script?
count=$(ls | wc -l)
next=$((count + 1))
echo "Next number is $next"
Bash Scripting
count=$(ls | wc -l) next=$((count + 1)) echo "Next number is $next"
Attempts:
2 left
💡 Hint
Command substitution gets the count, arithmetic expansion adds 1.
✗ Incorrect
The script counts files, then adds 1 using arithmetic expansion, then prints the result.