7

An alternative title for this question would be:

"command recognized in parent shell is not found in subshell"

I have this in a parent shell

source ~/.quicklock/ql.sh

and this command is recognized in my current shell:

ql_acquire_lock

then I run a script like so:

./script/tsc.sh

in that script we have:

#!/usr/bin/env bash
set -e;
ql_acquire_lock

I must be very confused about how shells work, because I thought a child shell/process would inherit the env of the parent, unless explicitly calling unset etc.?

Here is the error I get:

./scripts/tsc.sh: line 3: ql_acquire_lock: command not found
1
  • the command is a function, sorry should have specified that Mar 3, 2018 at 21:44

1 Answer 1

6

A child shell will inherit the environment of the parent shell.

The environment will contain what is exported by the parent shell.

If your shell function is not exported, it will not exist in the child's environment.

A bash shell function may be exported for use in a bash child shell using

export -f functionname

in the parent shell.

1
  • nice thanks that works, I have export all the functions, even ones I'd like to keep private but o/w that's good. Mar 3, 2018 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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