I'm trying to output the date and time, in a different time zone, in a bash shell script. Following this question, I did this:

#!/usr/bin/env bash

IL=":Asia/Jerusalem" date "+%a %b %d %R"
echo "Israel:    $IL"

which outputs this:

Wed Jun 19 15:12

Not only is the time incorrect for that time zone, but the output is incorrectly displayed on two lines.

I'd like it to output this:

Israel: Wed Jun 19 23:12

In other scripts, where the default time zone is fine (for example, in my .xinitrc to display the current system time), I've done:

date_str="$(date +"%a %b %d %R")";
topbar="Time: $date_str";
xsetroot -name "$top_bar";

What am I doing wrong this time?


You left out the TZ= part which will assign the timezone to the TZ environment variable for the date command:

TZ='Asia/Jerusalem' date '+%a %b %d %R'

The question you linked has a very detailed explanation of this.

In order to assign this output to a variable you need to use command substitution:

IL=$(TZ='Asia/Jerusalem' date '+%a %b %d %R')
| improve this answer | |

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.