Working with hours or minutes as numbers in bash has a nasty edge case: bash treats numbers with a leading zero as octal, and 08
and 09
are invalid octal numbers:
min=09
echo $(( min % 2 == 0 ))
bash: 09: value too great for base (error token is "09")
Use %-M
as the format directive -- it will return the minutes with no leading zero.
Additionally:
bash can do arithmetic, you don't need to call out to expr
if (( n % 2 == 0 )); then
echo "$n is even"
else
echo "$n is odd"
fi
and bash can do strftime
-like things. The %()T
directive in bash's builtin printf
allows the date format string inside the parentheses. In this simple case, you don't need to call out to date
.
minute=$(printf '%(%-M)T' -1) # -1 is a special value that means "now"
# or even
printf -v minute '%(%-M)T' -1
expr
is an external command likels
and like any command you are required to use spaces to separate the command from its arguments.