You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ]]
The text was updated successfully, but these errors were encountered:
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
rather than the current
But I could be mistaken or misunderstood something.
To Reproduce
test_string_expressions.sh
: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:The text was updated successfully, but these errors were encountered: