0
0
Bash Scriptingscripting~10 mins

String replacement (${var/old/new}) in Bash Scripting - Interactive Code Practice

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

Complete 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'
Aanimal
Bdog
Cand
Dcat
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'.
2fill in blank
medium

Complete 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'
Aapple
C/apple
Dbanana
Attempts:
3 left
💡 Hint
Common Mistakes
Using single slash / which replaces only the first occurrence.
Forgetting the double slashes and causing no replacement.
3fill in blank
hard

Fix 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'
A//
Bred/
C/
D/red/
Attempts:
3 left
💡 Hint
Common Mistakes
Using no slash or wrong placement of slashes.
Adding extra slashes causing syntax errors.
4fill in blank
hard

Fill 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'
A/
B//
C/dog
D//cat
Attempts:
3 left
💡 Hint
Common Mistakes
Using double slashes for first replacement.
Using single slash for replacing all occurrences.
5fill in blank
hard

Fill 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'
A//
B/
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.