Functions

Making Functions Return Values

The clue is to let the function run in its own sub process and let the value beecho-ed instead of return-ed as you'd do in most other programming languages.

functionget_number()
{
echo12;
}
echo"The number is"$(get_number)

Lambda functions in BASH

A great feature I love in Lisp, is lambda functions, a function which is applied to e.g. the values inside a loop. Doing this in BASH is so easy. I've created a simple (and yes, silly) example to illustrate this:

for el in black sabbath rocks; do
 double_length=$(
 # whatever is echo-ed here, is the return value of the lambda function.
 echo $(( $(echo $el | wc -c) * 2 ))
 )
 echo "The dobule length of $el is $double_length"
done

~ /home 🏠 ~ talks πŸ’¬ ~ bash ~ craftsmanship ~ db ~ dongxi ~ emacs ~ escenic ~ iam ~ java ~ js ~ language ~ latex ~ ldap ~ life ~ linux ~ llm ~ mac-os-x ~ mt-foo ~ network ~ norsk ~ python ~ quotes ~ running ~ security ~ travel ~ unix ~ various ~ vcs ~ webdesign ~ windows ~ discoveries ~ cv πŸ§™ ~

Licensed under CC BY Creative Commons License ~ πŸ“‘ RSS feed ~ βœ‰ torstein.k.johansen @ gmail ~ 🐘 @skybert@hachyderm.io ~ πŸŽ₯ youtube.com/@skybert

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /