bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The 'source x' command doesn't keep variables set by x when source o


From: Martin D Kealey
Subject: Re: The 'source x' command doesn't keep variables set by x when source output is piped into other command
Date: Thu, 14 Nov 2024 14:40:54 +1000

The disappearance of the variables that you export within your sourced file
is not a feature of the source command. That will happen to ANY command
that changes the shell's internal state, when run in a subshell.

The fact that pipeline components are implicitly run in subshells is
arguably not highlighted well enough, but it IS stated that all components
of a pipeline run in parallel, so it is logically deducible that this must
involve separate processes.

If you need to separate the output of `set -x` from your script's other
output, consider setting BASH_XTRACEFD:

exec 3>> /path/to/xtrace-logfile.txt
BASH_XTRACEFD=3
set -x
source your_file
set +x
exec 3>&-

or in recent versions of Bash:

exec {BASH_XTRACEFD}>> /path/to/xtrace-logfile.txt
set -x
source your_file
set +x
exec {BASH_XTRACEFD}>&-

(This lets Bash choose an available filedescriptor, rather than hard-coding
"3")

-Martin


reply via email to

[Prev in Thread] Current Thread [Next in Thread]