For your particular problem, this would also work if you are using Bash, shortening your code a little, by allowing you to omit the if
construct:
currentDate=${dateFromCLI-+%b%d%y}
The expression to the right of the equals sign is a special form of parameter expansion, the general form of which is:
${variableName-"default value"}
If the variable under the name variableName
is defined, the whole ${}
construct expands to the value of the variable.
If it is not defined however, then the construct expands to the default value, to the right of the literal hyphen (-
).
A variable is considered defined if it was assigned to previously and was not unset afterwards. It is even defined if an empty value was assigned to it.
If you have previously set dateFromCLI
to an empty value, you can use the same style of expansion, but with the -
substituted by :-
.
In this case, the default value is also chosen if the named variable is defined, but has a null value (empty string):
${variableName:-"default value"}
Reference: GNU Bash Reference Manual: Shell Parameter Expansion
date
! – Larry Jan 05 '19 at 15:11