Bash script is not inheriting its parent’s env

bashshellshell-script

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

Best Answer

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.

Related Question