EDIT: I originally assumed this was a scoping issue. I've updated the title to reflect the actual problem.
I've created an MRE below
#!/bin/bash
while getopts a OPTION
do
case ${OPTION} in
a)
echo "archive case"
ARCHIVE=true
;;
?)
echo "error"
;;
esac
done
if [[ "${ARCHIVE}" -eq true ]]
then
echo "it's archived"
else
echo "It's not archived"
fi
when I run './script.sh' I get the return value "it's archived", however when I run './script.sh -a' the output shows "archive case" and "It's archived."
Can someone tell me what is happening here? If the case statement is working why is the ARCHIVE variable being created regardless?
Thanks for your help.
set -vx
(and set +vxto turrn off). You can also use
set -xand
set +x. The
-vis "verbose" you'll see the code before any shell processing has happened, including the full text of loops, etc. The
-x(expanded?) prepends
+to the front of the line and all environment variables, etc are substituted substituted. You will see there, the exact command that the shell has interpeted from your code and is what is being execute. If you have cmd-substitutions (
var=$(myCmd) you'll likely see extra
+` char for each sub-level . – shellter Jan 20 '24 at 22:39set -vx
...set +vx
to see how that section is working. Try it! Just remember to delete or use theset +
version to turn off the feature. – shellter Jan 20 '24 at 22:48set -x
is a synonym forset -o xtrace
, and "xtrace" is likely short for "execution trace" (it shows the commands the shell executes, after all expansions etc.) – ilkkachu Jan 21 '24 at 13:57