If I define a bash function like this
test1 () {
(
touch /tmp/test1
rm /tmp/test1
read -rp "Enter anything: " anything
echo "Anything: ${anything}"
)
}
and set this option and trap
trap 'echo trapped' DEBUG;
shopt -s extdebug
and then execute it from an interactive bash session the read statement stops the job.
$ test1 () {
(
touch /tmp/test1
rm /tmp/test1
read -rp "Enter anything: " anything
echo "Anything: ${anything}"
)
}
$ test1
Enter anything:
[1]+ Stopped ( touch /tmp/test1; rm /tmp/test1; read -p "Enter anything: " anything; echo "Anything: ${anything}" )
$ fg 1
( touch /tmp/test1; rm /tmp/test1; read -p "Enter anything: " anything; echo "Anything: ${anything}" )
something
Anything: something
If I only have one command before the read then the function executes as expected.
This is a minimum example to reproduce the issue. I have work arounds but I'm trying to understand this behavior. Bash version is 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
John Bollinger
192k11 gold badges103 silver badges209 bronze badges
asked Jan 24, 2025 at 13:48
cementblocks
4,64521 silver badges26 bronze badges
lang-bash
localwithin a function:local anythinglocalis the "other means" I had in mind for that particular objective. Subshells do provide isolation for other aspects of the execution environment, so there might still be a reasonable use case there, but it depends on what exactly you need to isolate.local -xto create a local variable that is exported.