background
bash 4.2
I have two files envar
and main.sh
envar
...
# displays a spinner so that user knows the job is under processing.
spinner()
{
spin='-\|/'
i=0
while kill -0 $1 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\e[1;33m"
printf "\r${spin:$i:1}"
printf "\e[m"
sleep .1
done
}
...
main.sh
source envar
very_slow_and_dangerous_function &
pid=$!
spinner $pid
echo "$jobs done"
The problem
When someone press ctrl-c
during spinner $pid
the background shell becomes motherless.
I want both shells killed when ctrl-c
or any other force signals happen.
How do I use Trap here?
bash
but don't show a shebang or mention if you launch withbash main.sh
orsh main.sh
or something else.source
is not portable, it's a bashishm for the standard.
command. I doubt it will be relevant, but you never know.#!/bin/bash
. Thank you for the insight I didn't know source was bash only.