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

unbound variable check makes it impossible to optionally take into account an environment variable #1666

Closed
schlichtanders opened this issue Sep 6, 2023 · 3 comments

Comments

@schlichtanders
Copy link

schlichtanders commented Sep 6, 2023

Hi,
I today tried to migrate some adhoc scripts to a justfile and arrived at a bug/crucially missing feature/some misunderstanding:

test:
   #!/usr/bin/env bash
   set -euo pipefail
   if [[ -z "$MYVARIABLE" ]]; then
      DEFAULT="somedefault"
   else
      DEFAULT="$MYVARIABLE"
   fi

This raises the unbound variable error if MYVARIABLE does not exist as a environment variable
I would also be happy to know about a workaround (setting the environment variable manually before to empty is NOT a workaround for me - I want the final just call to be convenient)

@laniakea64
Copy link
Contributor

From man bash -

-u Treat unset variables and parameters other than the special parameters "@" and "*" as an error when
performing parameter expansion. If expansion is attempted on an unset variable or parameter, the
shell prints an error message, and, if not interactive, exits with a non-zero status.

So try removing u from the set line?

@casey
Copy link
Owner

casey commented Sep 6, 2023

You can also leave -u and use -z "${VAR:-}". ${VAR:-} will be empty if VAR is unset or empty, but won't trigger the unset variable check.

@schlichtanders
Copy link
Author

thank you so much for your help! that is it indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants