Skip to main content
added 16 characters in body
Source Link
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Or for a floating point number in zsh / ksh93 / yash:

$ echo "$((((${var1/:/*60+})-${var2/:/*60+})/60.))"
3.5

in zsh/ksh93/yash. If you have to use bash (which doesn't support floating point arithmetic):

$ awk 'BEGIN{print ARGV[1]/60}' "$(((${var1/:/*60+})-${var2/:/*60+}))"
3.5

To do the floating point part of the calculation by hand, though you might as well do the whole calculation in awk.

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Or for a floating point number:

$ echo "$((((${var1/:/*60+})-${var2/:/*60+})/60.))"
3.5

in zsh/ksh93/yash. If you have to use bash (which doesn't support floating point arithmetic):

$ awk 'BEGIN{print ARGV[1]/60}' "$(((${var1/:/*60+})-${var2/:/*60+}))"
3.5

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Or for a floating point number in zsh / ksh93 / yash:

$ echo "$((((${var1/:/*60+})-${var2/:/*60+})/60.))"
3.5

If you have to use bash (which doesn't support floating point arithmetic):

$ awk 'BEGIN{print ARGV[1]/60}' "$(((${var1/:/*60+})-${var2/:/*60+}))"
3.5

To do the floating point part of the calculation by hand, though you might as well do the whole calculation in awk.

added 293 characters in body
Source Link
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Or for a floating point number:

$ echo "$((((${var1/:/*60+})-${var2/:/*60+})/60.))"
3.5

in zsh/ksh93/yash. If you have to use bash (which doesn't support floating point arithmetic):

$ awk 'BEGIN{print ARGV[1]/60}' "$(((${var1/:/*60+})-${var2/:/*60+}))"
3.5

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.

Or for a floating point number:

$ echo "$((((${var1/:/*60+})-${var2/:/*60+})/60.))"
3.5

in zsh/ksh93/yash. If you have to use bash (which doesn't support floating point arithmetic):

$ awk 'BEGIN{print ARGV[1]/60}' "$(((${var1/:/*60+})-${var2/:/*60+}))"
3.5
Source Link
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k

Should be easy enough to do the calculation by hand:

$ printf '%02d:%02d\n' "$(((d=(${var1/:/*60+})-(${var2/:/*60+})),d/60))" "$((d%60))"
03:30

Here assuming $var1 is after $var2 or you'd get things like -02:-21 as a result.