Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Solaris / sh

I have a few functions defined in a file which gets loaded via

. ./some_file.sh 

When I start a subshell with

sh

All my function definitions are lost but when I do

env

I do see the source, is there an easy way to get them functional in my subshell?

share|improve this question

1 Answer 1

Functions are naturally propagated to subshells:

greet () {
  echo "hello, $1"
}
( echo "this is a subshell"; greet bob )

But they are not and cannot be propagated to independent shell processes that you start by invoking the shell under its name.

Bash has an extension to pass functions through the environment, but there's no such thing in other shells. While you can emulate the feature, it requires running code in the nested shell anyway. You might as well source your function definitions in the nested shell.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.