2

This question already has an answer here:

I want to enter the current date (ideally in the form YYYY-MM-DD hh:mm) as an option in a bash script:

I tried

/usr/local/bin/growlnotify -t "PMK" -m date

but then the variable -m inserts the string "date" in the output. How can I tell the script that it has to use the output value of the "date" command?

(I'm using MacOS X 10.6 and the growlnotify command is used to display a popup window with 2 strings ("PMK" and a second one where I'd like to get the current date/time) http://growl.info/ )

marked as duplicate by Gilles bash Oct 31 '14 at 23:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    /usr/local/bin/growlnotify -t "PMK" -m $(date "+%F %H:%M") – Costas Oct 31 '14 at 10:46
  • thanks, as I already commented below, this strangely adds the time string after PMK (like PMK 12:08) and only the date is processed by the -m option. But it's ok for me for the moment, this must be something with the growlnotify command – MostlyHarmless Oct 31 '14 at 11:14
3

writing date as an argument to another command will not get you the output of that command, just the string you typed.

In bash you can insert the result from a command by including it in $( ). That leaves that you need to specify form (format) that you want to get from date, and that can be deduced from man date (FORMAT section):

date '+%Y-%m-%d %H:%M'

This will give you a 24h clock (There are other ways to get this result, as Costas indicated, but this way you can easily change the characters between the year representation e.g. Germans often want /).

The full invocation would then be (there is no need to quote PMK, but don't forget the $( ...)):

/usr/local/bin/growlnotify -t PMK -m "$(date '+%Y-%m-%d %H:%M')"
  • thanks a lot for your help! In fact, your suggested answer (if I add the $( ... ) ) strangely adds the time after "PMK" and only the date in the second line... but for the moment it's ok for me. – MostlyHarmless Oct 31 '14 at 11:09
  • @Martin: Probably this is caused by the shell splitting the output of date at the space, which makes the time a separate argument to growlnotify. Try putting double quotes around: "$(...)" – crater2150 Oct 31 '14 at 12:21
  • 1
    Put the $( ... ) part in double quotes: "$( ... )" otherwise the shell simply splits date's output at the space, just as if you had typed -m 2014-10-31 13:22 – wurtel Oct 31 '14 at 12:21
  • 1
    @Martin I did not only forgot the $(...) in the final line but, like wurtel indictated, also double quotes around the whole to keep the space separated output together as one option argument for grwInotify. – Anthon Oct 31 '14 at 12:56
  • great, now it works perfectly. :-) – MostlyHarmless Oct 31 '14 at 13:03

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