Skip to content

Bug: String expressions bash variable set #173

Closed
@xaru8145

Description

@xaru8145

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 ]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions