I am writing a shell script that takes in a time (in minutes) and opens an online egg timer set for that amount.

#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
xdg-open http://e.ggtimer.com/"$TIME"min

When I run the above script it opens the web page http://e.ggtimer.com/5min I would like to set the time from the terminal. For example:

eggtimer 10

Would open http://e.ggtimer.com/10min

Thank you!

  • 5
    The first command line argument is $1 so TIME=$1 or just put $1 in your URL. – Joseph R. May 2 '14 at 0:07
  • 1
    @JosephR. Even if it seems too simple to you: Make that an answer so that it can be accepted and the question get marked done. – Hauke Laging May 2 '14 at 0:18
  • @HaukeLaging Seems slm already took care of that :) – Joseph R. May 2 '14 at 0:20
  • I swear I didn't even see your comment, I was already thinking of my ${VAR:-defaultval} post when I wrote it, just saw the commentary now. – slm May 2 '14 at 0:22
  • 1
    @slm No worries. My previous comment was sincere, not passive aggressive :) – Joseph R. May 2 '14 at 0:29

If you use the following feature of Bash variables where they're used if they have a value otherwise a default value is used instead you can code up your example like so:

$ more etimer.bash 
#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
xdg-open http://e.ggtimer.com/"$TIME"min

This will use an argument that's passed into etimer.bash if one is provided, otherwise will use 5 minutes.


$ ./etimer.bash

Results in this URL: http://e.ggtimer.com/5min

$ ./etimer.bash 10

Results in this URL: http://e.ggtimer.com/10min


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.