Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to replace the first occurrence of 'cat' with 'dog' in the variable.
Bash Scripting
animal="cat and cat" new_animal=${animal/$[1]/dog} echo "$new_animal"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double slashes // which replace all occurrences instead of one.
Replacing the wrong word like 'dog' instead of 'cat'.
✗ Incorrect
The syntax ${var/old/new} replaces the first occurrence of 'old' with 'new' in the variable 'var'. Here, 'cat' is replaced by 'dog'.
2fill in blank
mediumComplete the code to replace all occurrences of 'apple' with 'orange' in the variable.
Bash Scripting
fruits="apple banana apple" new_fruits=${fruits//$[1]/orange} echo "$new_fruits"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single slash / which replaces only the first occurrence.
Forgetting the double slashes and causing no replacement.
✗ Incorrect
Using double slashes // in ${var//old/new} replaces all occurrences of 'old' with 'new'.
3fill in blank
hardFix the error in the code to replace 'red' with 'blue' in the variable.
Bash Scripting
color="red green red" new_color=${color$[1]red/blue} echo "$new_color"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using no slash or wrong placement of slashes.
Adding extra slashes causing syntax errors.
✗ Incorrect
The correct syntax is ${var/old/new} with a single slash to replace the first occurrence of 'red' with 'blue'.
4fill in blank
hardFill both blanks to replace the first occurrence of 'cat' with 'dog' and then all occurrences of 'dog' with 'wolf'.
Bash Scripting
text="cat dog cat dog" step1=${text$[1]cat/dog} step2=${step1$[2]dog/wolf} echo "$step2"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double slashes for first replacement.
Using single slash for replacing all occurrences.
✗ Incorrect
Use single slash / to replace first occurrence and double slash // to replace all occurrences.
5fill in blank
hardFill all three blanks to replace 'apple' with 'orange', then 'banana' with 'grape', and finally 'grape' with 'melon' in the variable.
Bash Scripting
basket="apple banana banana apple" step1=${basket$[1]apple/orange} step2=${step1$[2]banana/grape} step3=${step2$[3]grape/melon} echo "$step3"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double slash for first replacement causing all to change.
Using single slash for later replacements causing only first to change.
✗ Incorrect
Use single slash / to replace first occurrence and double slash // to replace all occurrences. Here, first replacement is single, next two are all occurrences.