1

I have a bash script of the form:

cat  << EOF > stackoverflow_source.sh
#!/bin/bash
function f1 () {
         echo This is the first function.
}
function f2 () {
         echo This is the second function.
}
EOF

and its GUI with gtkdialog:

$ cat  << EOF > stackoverflow_gui.sh
#! /bin/bash
source stackoverflow_source.sh
export MAIN_DIALOG='
<window window_position="1" title="StackOverflow Question">
 <hbox>
  <button>
   <label>Function One</label>
   <action>f1</action>
  </button>
  <button>
   <label>Function Two</label>
   <action>f2</action>
  </button>
 </hbox>         
</window>'
gtkdialog  --program=MAIN_DIALOG
EOF

So after running:

$ source stackoverflow_source.sh

I get:

$ f1
This is the first function.

but when I click the corresponding button in the gui (even after sourcing the script) I get

sh: f1: command not found

When I export the functions with

$ export -f f1 && export -f f2

they do work in the gui (sh stackoverflow_gui and clicking their buttons, echos the text).

Is there a way to export every function in my stackoverflow_source.sh script? (In my real source script I have hundreds of functions and I do not want to do it one by one). Alternatively, is there a better way to do what I want?

1 Answer 1

2

This should work for you:

for func in $(declare -F | cut -f3 -d' ')
do
    export -f $func
done

Or, more concisely (but less readable):

export -f $(declare -F | cut -f3 -d' ')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.