Return values with return and echo in Bash
📖 Scenario: You are writing a small Bash script to calculate the sum of two numbers. You want to learn how to get values back from functions using return and echo.
🎯 Goal: Build a Bash script with a function that adds two numbers and returns the result using both return and echo. Then, display the results.
📋 What You'll Learn
Create a function called
add_numbers that takes two argumentsUse
return to send back the sum modulo 256Use
echo to print the full sumCall the function and capture both the return value and the echoed output
Print both captured values clearly
💡 Why This Matters
🌍 Real World
In real scripts, functions often need to send back status codes and data. Understanding how <code>return</code> and <code>echo</code> work helps you write better Bash scripts.
💼 Career
Many automation and DevOps tasks use Bash scripts. Knowing how to get values from functions is essential for debugging and building reliable scripts.
Progress0 / 4 steps