Bootstrap FreeKB - Linux Commands - Setting time zone using the TZ variable
Linux Commands - Setting time zone using the TZ variable

Updated:   |  Linux Commands articles

The TZ (time zone) environmental variable can be used to temporarily set a particular time zone. Almost always, the TZ environment variable will contain no value, meaning the TZ  environment variable is not setting the time zone, which can be seen using echo.

~]# echo $TZ

 

Also, env and printenv will not have have the TZ variable.

~]# env
~]# printenv

 

The date command can be used to display the current date, time and time zone.

~]$ date
Thu May 25 05:17:12 CDT 2023

 

Export can be used to temporarily change the time zone using the time zone abbreviation such as EST (Eastern Standard Time) or GMT (Greenwich Mean Time) or UTC (Universal Time Coordinated).

export TZ=GMT

 

Or, contient/city can be used.

export TZ=US/Central

 

And now the date should be in the selected time zone.

~]$ date
Thu May 25 10:18:14 GMT 2023

 

And echo $TZ should show the time zone.

~]# echo $TZ
GMT

 

Likewize, both env and printenv should have TZ too.

~]# env | grep TZ
US/Central

~]# printenv | grep TZ
US/Central

 

unset can be used to return to the systems default time zone.

unset TZ

 

However, using the time zone abbreviation such as CST or EST or GMT or UTC in a bash shell script has an unexpected result. Take for example this bash shell script which set the time zone to EST.

#!/bin/bash
date
export TZ=EST
date
unset TZ

 

When I run the script, the time zone is changes, but my time is not adjusted.

~]$ bash testing.sh
Thu May 25 05:23:32 CDT 2023
Thu May 25 05:23:32 EST 2023

 

On the other hand, when I use contient/city in the bash shell script.

#!/bin/bash
date
export TZ=America/Detroit
date
unset TZ

 

The time zone and time are properly adjusted.

~]$ bash testing.sh
Thu May 25 05:30:31 CDT 2023
Thu May 25 06:30:31 EDT 2023

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fffdd9 in the box below so that we can be sure you are a human.