Call C function in Bash script

11,860

Solution 1

Bash supports loadable builtins. You might be able to make use of this to do what you want. See the files in your /usr/share/doc/bash/examples/loadables (or similar) directory.

Solution 2

It even possible to use C data structures ;)

This is ctypes.sh, a foreign function interface for bash. ctypes.sh is a bash plugin that provides a foreign function interface directly in your shell. In other words, it allows you to call routines in shared libraries from within bash.

Check out https://github.com/taviso/ctypes.sh ;)

Solution 3

The simplest way to do this is to write a simple program that collects the input, feeds it to the function, then prints the result. Why don't you tell us what you are attempting to accomplish and perhaps we can suggest an easier way to "skin this cat".

Solution 4

No.

You can't access a function internal to the shell binary from the shell if it is not exported as a shell function.

Solution 5

No, you'll have to write a short C program, compile it and call it from the shell.

Share:
11,860
l0b0
Author by

l0b0

Author, The newline Guide to Bash Scripting (https://www.newline.co/courses/newline-guide-to-bash-scripting). Hobby (https://gitlab.com/victor-engmark) & work software developer.

Updated on July 24, 2022

Comments

  • l0b0
    l0b0 almost 2 years

    Related to question 3451993, is it possible to call a function which is internal to subst.c (in the Bash source code) in a Bash script?

  • l0b0
    l0b0 almost 14 years
    I'm trying to call a function which is internal to Bash, but it looks like it's not compiled into a separate shell command. Please see the link for more info.
  • Jay
    Jay almost 14 years
    It's very difficult to do this and is usually very unreliable. You're better off trying to get what you want a different way.
  • msw
    msw almost 14 years
    @l0b0: restating the question is not equivalent to saying what you are trying to accomplish.
  • l0b0
    l0b0 almost 14 years
    Good idea. Unfortunately grep subst /usr/share/doc/bash/examples/loadables/* returned nothing.
  • SourceSeeker
    SourceSeeker almost 14 years
    @l0b0: That just means you have to write your own. Also, grep subst /usr/include/bash/* gives /usr/include/bash/shell.h:#include "subst.h", grep shell.h /usr/share/doc/bash/examples/loadables/* gives a bunch. Unfortunately, grep prompt /usr/share/doc/bash/examples/loadables/* gives nothing, but grep prompt /usr/include/bash/subst.h does look interesting.
  • user2284570
    user2284570 about 7 years
    The simplest way to do this is to write a simple program that collects the input, feeds it to the function, then prints the resultHow do you do this on chrome os? It is the case where all partitions are mounted with noexec except for the rootfs which is write protected.
  • Jay
    Jay about 7 years
    Never used Chrome OS. It sounds like they don't allow you to execute anything. If that's true you won't have a simple hack. You'd probably have to write a plugin for chrome.
  • mljrg
    mljrg almost 3 years
    This is the most interesting project I have seen in the past couple of years. Thanks for you answer!