Last active
March 26, 2022 14:44
-
-
Save sd031/10276e45bf15e19a876e15f7fd8ad6ea to your computer and use it in GitHub Desktop.
Learn Linux Basics | part 5: Shell Scripting functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Funtions: | |
Writing Functions: | |
Normal function: | |
get_name() { | |
echo "John" | |
} | |
echo "You are $(get_name)" | |
Function that accepts parameters and process it: | |
argunets and their meaning | |
$# Number of arguments | |
$* All arguments | |
$@ All arguments, starting from first | |
$1 First argument | |
myfunc() { | |
echo "hello $1" | |
} | |
# Same as above (alternate syntax) | |
function myfunc() { | |
echo "hello $1" | |
} | |
myfunc "Sandip" | |
Function returning values: | |
myfunc() { | |
local myresult='some value' | |
echo $myresult | |
} | |
result="$(myfunc)" | |
Raising errors: | |
myfunc() { | |
return 1 | |
} | |
if myfunc; then | |
echo "success" | |
else | |
echo "failure" | |
fi | |
What next? | |
Next I will cover loops in shell scripting | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment