kvz / bash3boilerplate

Templates to write better Bash scripts
http://bash3boilerplate.sh
MIT License
2.12k stars 198 forks source link

Default="${HOME}/..." not properly expanded #143

Open guw opened 3 years ago

guw commented 3 years ago

I might be doing something wrong but it looks like variable expansion is not working for default values in my sell environment (Big Sur, Fish shell).

I have

  -s --some-dir [arg]       Location of directory. Default="${HOME}/some/dir"

Further down I have:

debug "arg_s: ${arg_s}"
debug "HOME: ${HOME}"

[[ -d "${arg_s:-}" ]] || {
    error     "No directory found at: ${arg_s:-}"
    exit 1
}

Here is my output:

$  LOG_LEVEL=7  ./myscript -- test

[    debug] arg_s: ${HOME}/some/dir
[    debug] HOME: /Users/myusername
[    error] No directory found at: ${HOME}/some/dir

The interesting thing is that it really prints out ${HOME} instead of /Users/myusername. However, when I use ${HOME} directly in the script it works.

One observation from my shell:

$ echo "${HOME}"
fish: Variables cannot be bracketed. In fish, please use "$HOME".
echo "${HOME}"
       ^

$ echo "$HOME"
/Users/myusername
mstreuhofer commented 3 years ago

Well I can tell you that it has nothing to do with your shell. I tested it with BASH and the same happens. The code parsing the usage string into getopts arguments is not sophisticated enough or maybe it was never intended to support that. But don't let me stop you from scratching that itch of yours. We all would certainly welcome this new feature. :-)

guw commented 3 years ago

Oh, so I read the documentation wrong. Sorry about that. When reading https://github.com/kvz/bash3boilerplate/blob/986fec809095d5df2d71c08262ab9a705ec373f2/main.sh#L136 I assumed it would work in the default.

mstreuhofer commented 3 years ago

Nice catch. Might be that this comment is from a time before the big rewrite of the usage string parsing. Of course this means that either the code is wrong or the comment.