1

I am trying to convert a GMT time to AEST Time with the below code and it is working fine. date --date='TZ="GMT" 2017/07/24 12:00:00' "+%Y-%m-%d %H:%M:%S"

When I pass the date variable as a parameter to the above query like below:

RequestDate="2017/07/24 12:00:00"

date --date='TZ="GMT" ${RequestDate}' "+%Y-%m-%d %H:%M:%S".

I am getting the error:

date: invalid date `TZ="GMT" ${RequestDate}`.

I tried with multiple encapsulations of single quotes and double quotes.

1

There are different kinds of quotes in the shell, and different possible solutions. The other answer is correct, values in single quotes are not expanded. But you want the TZ as part of the argument, and you want the argument to contain quotes. You can achieve this by adding a part in single quotes and a part in double quotes without separator.

date --date='TZ="GMT" '"${RequestDate}" "+%Y-%m-%d %H:%M:%S"

You can also use a backslash

date --date="TZ=\"GMT\" ${RequestDate}" "+%Y-%m-%d %H:%M:%S"
  • Well, in that case I'm obviously wrong. Sorry for the noise. My excuse is that I'm not ordinarily a GNU tool user. – Kusalananda Aug 6 '18 at 19:21
  • Thanks RalfFriedl. Both the commands worked for me. I had been scratching my head changing the quotes. :) – MAK33 Aug 7 '18 at 0:41
2

If you put a variable in a single-quoted string, the shell doesn't expand the variable. Try:

$ TZ=GMT date --date "${RequestDate}" "+%Y-%m-%d %H:%M:%S"

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.