How to suppress leading zero in date output?

3.1k Views Asked by At

I have this code:

printf -v s '%(%S)T' -1 # grab the current second
if ((s == 0)); then
  # at the top of the minute, run some code
fi

This code throws an error on the eighth and ninth second of every minute:

bash: ((: 08: value too great for base (error token is "08")
bash: ((: 09: value too great for base (error token is "09")

How can I rectify this? Basically, we need to suppress the leading zero in the date output generated by printf.

1

There are 1 best solutions below

0
On BEST ANSWER

Use a - prefix in the format string, thus:

printf -v s '%(%-S)T' -1

This suppresses the leading zero.

A more generic way of solving this is to specify the base in Bash arithmetic this way, while keeping the printf command unchanged:

if ((10#$s == 0)); then

Related post on Unix & Linux Stack Exchange: