0

I'm trying to use a bash function from find. I know that I need to export the function. When I do this from the command line it works. When I do it from within a bash script it doesn't. the code is listed below. if you execute it doesn't work. if you source it, then it does. What is interesting, is that once you source it, the script will then work because it's defined at the time of the script invocation.

#!/bin/bash
function doit {
    echo args "<$@>"
}

doit a b c

export -f doit

find . -maxdepth 1 -exec sh -c 'doit "$@" ' {}  \+

here's the error I see:

> ./x.sh
args <a b c>
.: doit: command not found

1 Answer 1

1

You are using bash's export but using plain sh which may not understand it (which may be symlinked to bash, not necessarily true on all platforms).

Use bash to exec. Change:

find . -maxdepth 1 -exec sh -c 'doit "$@" ' {}  \+

to

find . -maxdepth 1 -exec bash -c 'doit "$@" ' {}  \+
Sign up to request clarification or add additional context in comments.

1 Comment

gah - wished I seen that earlier. really odd that this works at the command line but not once I put it in the script. thanks!

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.