Bird
0
0

You want to create a bash function sum that takes two numbers and prints their sum. Which function definition correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - Functions
You want to create a bash function sum that takes two numbers and prints their sum. Which function definition correctly does this?
Afunction sum { echo $1 + $2; }
Bsum() { echo $1 + $2; }
Csum() { echo $(($1 + $2)); }
Dsum() { echo $1 $2; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand arithmetic in bash functions

    To add numbers, use arithmetic expansion $(()) inside echo.
  2. Step 2: Evaluate each option

    sum() { echo $(($1 + $2)); } correctly sums $1 and $2 using $(($1 + $2)). Options A and B print strings with plus sign, not sum. sum() { echo $1 $2; } prints numbers without adding.
  3. Final Answer:

    sum() { echo $(($1 + $2)); } -> Option C
  4. Quick Check:

    Use $(()) for arithmetic in bash functions [OK]
Quick Trick: Use $(()) to perform math inside bash functions [OK]
Common Mistakes:
MISTAKES
  • Printing variables without arithmetic expansion
  • Omitting parentheses for function definition
  • Using 'function' without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes