https://github.com/christophgil/bash_return
Bash builtin for fast returning numeric and string values from Bash functions
https://github.com/christophgil/bash_return
bash bash-builtin bash-scripting bashbuiltin extension functions performance plugin plugins return return-value
Last synced: 3 months ago
JSON representation
Bash builtin for fast returning numeric and string values from Bash functions
- Host: GitHub
- URL: https://github.com/christophgil/bash_return
- Owner: christophgil
- Created: 2025-01-07T21:57:13.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-01-10T23:02:52.000Z (5 months ago)
- Last Synced: 2025-01-28T14:46:05.848Z (5 months ago)
- Topics: bash, bash-builtin, bash-scripting, bashbuiltin, extension, functions, performance, plugin, plugins, return, return-value
- Language: Shell
- Homepage:
- Size: 60.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bash_return
# Summary
This Bash extension written in C allows script functions to return numeric or string values.
Status: Experimental
Generally, two methods can be used to return results from functions to the caller of the function.
(I) Using command substitution to capture the output may cause poor performance.
(II) Storing the result in a global variable is less compact and requires two statements.With this Bash extension, functions can be used in both ways depending on a command line switch.
See [Details](./motivation.md).# Installation
First [Install dependencies](./INSTALL_DEPENDENCIES.md)
Compile:
compile_C.sh bashbuiltin_cg_return.c
# Usage
The compiled builtins must be loaded once.
enable -f /home/cgille/compiled/bashbuiltin_cg_return.so init_retval set_retval
The Bash builtin *init_retval* must be called at the top of the function body.
The return value of the function is set with the builtin *set_retval*.square(){
init_retval
: Do something
set_retval $(($1*$1))
}When the function is called with the switch -$, then the returned value is obtained from a variable
whose name is stored in the global variable *RETVAL*. Usage of a global variable is possible
because Bash is not multi-threaded. Normally, dollar signs need to be quoted in UNIX shells. Note
that the dollar sign does not need to be quoted here because it is followed by a space. This rather
unusual command line switch is unlikely to be used already. Note that *-$* must be the first
parameter. It is shifted out by *init_retval* and will not be in the argument list any more.square -$ 3
echo "The Square of 3 is ${RETVAL}"Without the option *-$*, the result is printed to the standard output.
square 3
# Example with Arrays
my_seq(){
init_retval
local int n=$1
local aa=()
for((i=0;i