0
0
Bash Scriptingscripting~10 mins

Return values (return and echo) 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 return the value 5 from the function.

Bash Scripting
my_func() {
  [1] 5
}
my_func
Drag options to blanks, or click blank then click option'
Aprint
Becho
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using echo instead of return to send back a value.
Using exit inside the function which stops the whole script.
2fill in blank
medium

Complete the code to print the string 'Hello' from the function.

Bash Scripting
greet() {
  [1] "Hello"
}
greet
Drag options to blanks, or click blank then click option'
Aecho
Bprint
Cexit
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using return to try to print text.
Using print which is not a bash command.
3fill in blank
hard

Fix the error in the function to correctly return the exit status 3.

Bash Scripting
check_status() {
  [1] 3
}
check_status
status=$?
echo $status
Drag options to blanks, or click blank then click option'
Aecho
Bprint
Cexit
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using echo instead of return to set exit status.
Using exit which terminates the script.
4fill in blank
hard

Fill both blanks to create a function that prints 'Done' and returns exit status 0.

Bash Scripting
finish() {
  [1] "Done"
  [2] 0
}
finish
status=$?
echo $status
Drag options to blanks, or click blank then click option'
Aecho
Bexit
Creturn
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using exit instead of return which stops the script.
Using print which is not a bash command.
5fill in blank
hard

Fill all three blanks to create a function that prints the argument, returns 1 if argument is 'fail', else returns 0.

Bash Scripting
result() {
  [1] "$1"
  if [[ "$1" == "fail" ]]; then
    [2] 1
  else
    [3] 0
  fi
}
result "$1"
exit_status=$?
echo $exit_status
Drag options to blanks, or click blank then click option'
Aecho
Breturn
Cexit
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using exit inside the function which stops the whole script.
Using print which is not a bash command.
Using echo instead of return to set exit status.