Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: String expressions bash variable set #173

Closed
xaru8145 opened this issue Jan 21, 2025 · 1 comment · Fixed by #174
Closed

Bug: String expressions bash variable set #173

xaru8145 opened this issue Jan 21, 2025 · 1 comment · Fixed by #174
Labels
bug Something isn't working

Comments

@xaru8145
Copy link

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

  1. 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
  1. Execute the script
  2. 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 ]]
@xaru8145 xaru8145 added the bug Something isn't working label Jan 21, 2025
@bobbyiliev bobbyiliev mentioned this issue Jan 21, 2025
8 tasks
@bobbyiliev
Copy link
Owner

Thank you for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants