I have a function that seems to break when I turn on errors with set -e
. I suspect that it is working as intended but I can't see what I'm doing wrong.
Here is the function:
#!/usr/bin/env bash
set -e
my_function() {
base_dir="/tmp/test"
if [ ! -d $base_dir ]; then
echo "Creating $base_dir"
mkdir -p $base_dir
touch $base_dir/some-file.txt
fi
is_edited=$(grep "foo" $base_dir/some-file.txt)
if [ ! -z $is_edited ]; then
cd $base_dir
echo "Doing stuff"
docker run --rm debian ls
fi
echo
echo "Done"
echo
}
my_function
With set -e
flipped on, the function returns seemingly without hit the echo statements. With set -e
turned off, the echo statements get hit. What is happening and how do I fix this?