taviso/ctypes.sh
FreeA foreign function interface for bash.
FreeFree tier
About taviso/ctypes.sh
ctypes.sh is a bash plugin that provides a foreign function interface (FFI) directly in the shell, allowing users to call routines in shared libraries from within bash scripts. It uses libffi to dynamically invoke functions, enabling tasks that were previously impossible or required external helper programs. Example uses include calling libm's sin function, using GTK natively in shell scripts, and building a high-performance HTTP daemon. The plugin supports automatic struct parsing when debug symbols are available. It is open source under the MIT license and requires compilation from source with dependencies such as libffi, libelf, and libdw.
Key Features
Call routines in shared libraries directly from bash using dlcall and dlopen
Supports libffi for dynamic function invocation
Automatic struct parsing with debug symbols (libdwarf/libdw)
Enables using libraries like GTK natively in shell scripts
Can be used to implement high-performance HTTP daemon in bash
Open source under MIT license
Installable from source via autotools build system
Pros & Cons
Pros
- Enables direct calling of C library functions from bash without external helpers
- Eliminates need for separate compiled helper programs for many tasks
- Allows use of complex libraries like GTK in shell scripts
- Open source with MIT license, free to use and modify
- Supports automatic struct parsing with debug symbols, reducing boilerplate
Cons
- Requires compilation from source with multiple dependencies (libffi, libelf, libdw)
- Installation process may be complex for non-developers
- Documentation is mostly limited to the GitHub wiki and examples
- Limited to bash environment and may not work with all bash versions
Best For
Calling shared library functions (e.g., puts, sin from libm) from bashUsing GTK or other native GUI libraries in shell scriptsWriting high-performance HTTP daemon entirely in bashExtending bash scripts to accomplish tasks that previously required external helper programs
FAQ
What is ctypes.sh?
ctypes.sh is a bash plugin that provides a foreign function interface (FFI), allowing you to call routines in shared libraries directly from bash scripts.
How do I install ctypes.sh?
Clone the repository, run ./autogen.sh, ./configure, make, and sudo make install. Prerequisites include libffi, libelf, and libdw (optional for automatic struct support).
Can I use GTK with ctypes.sh?
Yes, ctypes.sh can be used to use GTK natively in shell scripts, enabling GUI applications written entirely in bash.
Is ctypes.sh free?
Yes, it is open source under the MIT license, free to use, modify, and distribute.
What are some examples of using ctypes.sh?
Examples include calling puts to print strings, calculating sin(PI/2) using libm, and implementing a high-performance HTTP daemon in bash.