Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.

function time_seconds()
  echo "Counting to $1 seconds"
  time_func $1

function time_minutes()
  echo "Counting to $1 minutes"
  time_func $1*60

function time_hours()
  echo "Counting to $1 hours"
  time_func $1*60*60

function time_flexible()  # accepts flexible input hh:mm:ss (does not work)
    echo "Counting to $1"
    secs=$(time2seconds $1)
    time_func $secs

enter image description here

I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:

sleep 5h 22m 44s

I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:

stopwatch 5h 22m 44s

How would I go about accomplishing this?

p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.


Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):

to_seconds () {

    for ts do
        case $ts in
            *h) seconds=$(( seconds + 60*60*"${ts%h}" )) ;;
            *m) seconds=$(( seconds +    60*"${ts%m}" )) ;;
             *) seconds=$(( seconds +       "${ts%s}" )) ;;

    printf '%s\n' "$seconds"

The loop in the function will loop over the given arguments.

The ${ts%h} parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.

If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.

You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.


$ to_seconds 5h 22m 44s
$ to_seconds 5h 22m 44
$ to_seconds 44
$ to_seconds 10
$ to_seconds 10m
$ to_seconds 10h
$ to_seconds 2i
bash: seconds +       2i: value too great for base (error token is "2i")

To verify that a given number is an integer, see

  • This doesn't count down though, and only does seconds? – Anonymous Apr 4 at 23:18
  • @AnonymousUser The issue you had was about unifying three different functions into one. You showed a time_flexible() function that you said did not work. I have given you a way of calculating seconds from hours, minutes and seconds (given on the format that you prescribed). I'm assuming that you would either call this (with your own modifications added), or incorporate the already existing counting-down-code into it. I'm not going to write code that you already have and that you seem to have no issue with. – Kusalananda Apr 5 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.