0
0
Bash Scriptingscripting~20 mins

Backticks and $() for command substitution in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Command Substitution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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"
AToday is 2024-06-15
BToday is `date +%Y-%m-%d`
CToday is $(date +%Y-%m-%d)
DSyntax error
Attempts:
2 left
💡 Hint
Backticks run the command inside and replace it with the output.
💻 Command Output
intermediate
2: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"
ANumber of files: $(ls | wc -l)
BNumber of files: ls | wc -l
CNumber of files: 5
DSyntax error
Attempts:
2 left
💡 Hint
The $() runs the command inside and replaces it with the output.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in command substitution
Which option contains a syntax error in command substitution?
Aoutput=$ls -l
Boutput=$(ls -l)
Coutput=`ls -l`
D`l- sl`=tuptuo
Attempts:
2 left
💡 Hint
Command substitution requires backticks or $() around the command.
🔧 Debug
advanced
2: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"
ANo error, runs fine
BMissing closing quote for pattern
Cgrep command is invalid
DMissing closing backtick
Attempts:
2 left
💡 Hint
Check the quotes inside the backticks carefully.
🚀 Application
expert
3: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"
ASyntax error
BNext number is count + 1
CNext number is $(ls | wc -l + 1)
DNext number is 6
Attempts:
2 left
💡 Hint
Command substitution gets the count, arithmetic expansion adds 1.