Issue
I wrote the following code in the bash shell. It's supposed to take the positional parameter and, if it starts with a dash "-", print an error message. For some reason the if
statement always gets skipped. It only works if I literally input -*
.
I get the impression that the fix has something to do with the $
.
EXECNAME=$1
if [ "$EXECNAME" = "-*" ]; then
echo "error: invalid executable name"
fi
Solution
You can use double square brackets [[
and ]]
without use of quotes on matching pattern for glob support in BASH:
[[ "$EXECNAME" = -* ]] && echo "error: invalid executable name"
Answered By - anubhava Answer Checked By - Timothy Miller (WPSolving Admin)