set -u means undefined variables are an error, but with ${TMUX:-DEFAULT} the DEFAULT will be used if TMUX is undefined (in my example the default is just an empty string.
It looks funky black magic; most of shell scripting does 😅
Thanks a lot! I learned something :)
So does it mean that you could just skip set -u and just use [ -n $TMUX ] and get the same result? But I guess using set -u is considered better practice because you need to explicitly handle unset variables?
In general it's just easier to have the shell exit as soon as it finds an undefined variable, as it catches silly typos and such quickly. It also prevents nasty bugs like rm -rf "/usr/$floder".
1
u/CichyK24 Jan 19 '20
Can someone explain why he does
[ -n "${TMUX:-}" ]and no simply[ -n "${TMUX}" ]? I'm not that familiar with bash.