It is not hard to set a default variable in bash. E.g: How to write a bash script that takes optional input arguments?
But, these methods fail when using set -euxo pipefail. What methods are compatible with the -u flag?
Example:
set -euxo pipefail
foo=${2:test}
echo $foo
for Bash GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
echo "${1:-foo}"and it worked fine. What version of Bash are you using? I think I read that this behaviour can vary across versions. I'm using 5.0.:-or hyphen-. A colon by itself:selects a substring, e.g. withset -- hello world; test=2, the result would berld.set -eis generally regarded in the bash community as a bad idea. See BashFAQ #105 describing the pitfalls withset -e, and BashFAQ #112 describing best practices for working withset -u. I strongly advise explicit error handling in place ofset -e, and more weakly advise static checking (shellcheck is widely integrated into, or available via a plugin for, many editors now) in place ofset -u.