Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sd031/10276e45bf15e19a876e15f7fd8ad6ea to your computer and use it in GitHub Desktop.
Save sd031/10276e45bf15e19a876e15f7fd8ad6ea to your computer and use it in GitHub Desktop.
Learn Linux Basics | part 5: Shell Scripting functions
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