1

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
Israel:

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?

Michael A
  • 1,593
  • 5
  • 19
  • 33

1 Answers1

2

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')
jesse_b
  • 37,005