I see several questions here.
-
“Can I write something that actually reflects this logic”
Yes. There are a few ways you can do it. Here’s one:
if [[ "$1" != "" ]]; then DIR="$1" else DIR=. fi -
“What is the difference between this and
DIR=${1-.}?”The syntax
${1-.}expands to.if$1is unset, but expands like$1if$1is set—even if$1is set to the empty string.The syntax
${1:-.}expands to.if$1is unset or is set to the empty string. It expands like$1only if$1is set to something other than the empty string. -
“Why can’t I do this?
DIR="$1" || '.'”Because this is bash, not perl or ruby or some other language. (Pardon my snideness.)
In bash,
||separates entire commands (technically it separates pipelines). It doesn’t separate expressions.So
DIR="$1" || '.'means “executeDIR="$1", and if that exits with a non-zero exit code, execute'.'”.