Linux CLI 58 ๐ง shell functions part 2
a - shell functions return codes
function return codes are used to indicate the success or failure of a function's execution.
also known as exit statuses
a return code of 0 indicates successful execution
while any other value indicates an error or failure.
return 0 โ returns 0 for successful execution
return 1 โ return 1 for non succesful execution
check_success $? โ $? captures the return code
b - shell function exit command and local variables
exit command terminates a script execution based on some condition
You can also return code using exit commad
exit 0 โ terminates execution with return code 0
exit 1 โ terminates execution with return code 1
local variables are used to define variables within a specific scope or context inside a function.
These variables are not accessible outside the function
They do not affect or interfere with global variables of the same name.
local result=$1 โ declares a local variable with the name result
c - shell functions error handling
Error handling is an essential aspect of writing robust shell scripts
strategies for implementing effective error handling in functions within your shell scripts:
using return codes
using set -e โ automatically fails on error
using trap โ Trap errors and handle them manually
Using set -u โ automatically fails on undefined variables
set -e โ exits immediately if a command exits with a non-zero status.
set -u โ fails on an undefined variable.
trap 'echo "An error... โ traps errors and handle them manually.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!