Closed
Description
Describe the bug
The section String Expression showcases an example with -v
to check if a shell variable is set that I think is used incorrectly.
For the -v condition to work as expected it should be used as
[[ -v VARNAME ]]
rather than the current
[[ -v ${varname} ]]
But I could be mistaken or misunderstood something.
To Reproduce
- Create the following bash script
test_string_expressions.sh
:
#!/bin/bash
VARNAME="Test"
if [[ -v ${VARNAME} ]]; then
echo "Shell variable is set. Value: ${VARNAME}"
else
echo "Shell variable is not set"
fi
- Execute the script
- See script output: Shell variable is not set. But the variable is set.
Expected behavior
The script should output: Shell variable is set. Value: Test.
The -v
operator in Bash checks if a variable name exists, not its value. Specifically, one should pass the name of the variable directly to -v, not its dereferenced value (${VARNAME}). Here's how i think it should be:
[[ -v VARNAME ]]